We help IT Professionals succeed at work.

C# Generics  - How to create a Generic Property

JElster
JElster asked
on
2,743 Views
Last Modified: 2012-05-09
Hi..
How can I create a Generic Property  in a class ?  That can hold anytime of Generic List?

like  List<Customer>  or  List<Orders>   or   List<CustomObject>

thanks

Comment
Watch Question

Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
.HI JElster,

this way : List<List<Object>> list;
and initialize with new List<List<Customer>>() and so on.
Regards,
Chinmay

Author

Commented:
When I try and set it I get

Can not implicity convert type Generic.List<Anoynomous Type> to Generic.List<Object>



thanks
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hi... after a little research in IDE and somewhat search on Google brought this up :
http://stackoverflow.com/questions/847538/nested-generic-collections-how-to-implement-reference-from-item-to-container
Seems if you are using C# than you might take the co-contra variance route otherwise you have to follow the custom class method shown in above link.
BTW, an interesting read on co-contra variance is here : http://bartdesmet.net/blogs/bart/archive/2009/04/13/c-4-0-feature-focus-part-4-generic-co-and-contra-variance-for-delegate-and-interface-types.aspx
Sorry my earlier answer was not very well informed.
Regards,
Chinmay

Could you not use List<Object> ? What about an interface?
or maybe......
class SomeClass<T> 
    { 
    } 
 
    class C 
    { 
        void Add<T>(SomeClass<T> item) 
        { 
                Type type = typeof(SomeClass<T>); 
                if (!list.ContainsKey(type)) 
                        list[type] = new List<SomeClass<T>>(); 
                List<SomeClass<T>> l = (List<SomeClass<T>>)list[type]; 
                l.Add(item); 
        } 
 
        public void Method<T>(SomeClass<T> obj) 
        { 
                Add(obj); 
        } 
        Dictionary<Type, object> list = new Dictionary<Type, object>(); 
    } 





// to test it

class Program 
    { 
        static void Main(string[] args) 
        { 
                C c = new C(); 
                SomeClass<int> sc1 = new SomeClass<int>(); 
                SomeClass<String> sc2 = new SomeClass<String>(); 
                c.Method(sc1); 
                c.Method(sc2); 
                c.Method(sc1); 
                c.Method(sc2); 
        } 
    }

Open in new window

or just:

List<object>  

Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
thanks...I'll try it.....
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.