Click here to Skip to main content
15,038,643 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Please have a look at this code block:
using Local = Local.A
using Global = Global.A

public void TreeViewBinding<T>(T t) where T : Local
                                    where T : Global
   //My code goes here

I know this code does not work. So my question is :

" Is there any other ways to archive this method? Given that the two classes A do not reside in the same namespace and I also note that they even do not have implement any common Interface."

At the moment, here's my solution. these aren't convenience ways:

public void TreeViewBinding(Local localParam)
    //My code goes here

public void TreeViewBinding(Global globalParam)
    //My code goes here
Updated 5-May-14 18:49pm
Why would you try to do such a weird thing? Even if perfect world... A.Local and A.Global could be unrelated classes, so the set of possible real type to be used as a generic parameter and meeting the constraint would be empty. It all does not seem to makes sense. What would be your purpose?
Benjamin Nguyễn Đạt 6-May-14 1:08am
Thanks for your consideration. My purpose here is just want to write a common method to use with both of two Type ( Local.A , Global.A these 2 classes have exactly the same properties ). This method will get the data on the LOCAL or GLOBAL depends on your passed in params. I'm working with NHibernate.
Don't you see that it would not generally make sense? Okay, please give me some sample definition of Local.A and Global.A (simplified as much as possible), and I'll explain it to you. And perhaps you could write one or two lines in the sample implementation of TreeViewBinding using one or two members of Local.A and Global.A. It would be good if you explain your purpose, if you can. On your example, I'll explain you the matter of the problem.

Benjamin Nguyễn Đạt 6-May-14 2:45am
Hi SA, sorry you that my English not quite good to get all your points.

The Global.A and Local.A, they define the same properties ( e.g Name, Age ).
First, I decided to write a generic method as also described above, but then I realized that it would be impossible for the T to derived from both Local.A & Global.A.
So, I went with the overload solution.

My question is :
- Is there any way for the T to derive from more than one class in the generic method?

Once you give me the answer yes or no, I could solve it myself.

Thanks for your consideration.

1 solution

Are you sure you understand the intended use for constraints on generics? Are Global and Local types (classes)?
Benjamin Nguyễn Đạt 6-May-14 2:17am
Yes they're both class.
Not answer at all, just the questions. And pretty much irrelevant to the problem.
Such posts are considered as abuse.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900