.Net 2.0+ Reflect Generic List Type

hi there,

List<MyClass> list = new List<MyClass>();
Type t = list.GetType();

Output: System.Collections.Generic.List`1[MyClass]

How can I get MyClass into "Type t" via the "list" variable?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I do not exactly understand what you problem is. If you want to have a "Type t" with MyClass, just use typeof(MyClass), however if you want to get a type of the item in the List<MyClass> list, you have to get it first, so do something like list [0].GetType

Does this help???

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ValimaiAuthor Commented:
list [0].GetType()....
I was hoping that i could get it even if the collection is empty. It seems odd that I could not find a property of the List<> object which revealed the type it contains.
ValimaiAuthor Commented:
I ended up fixing the structure of my code using a generic class and did not need to use your solutions. However, they were quite valid in the context of the example. Thank you for your help!
Well, resently I had a situation similar to yours. I had a generic list (List <EItem>) and had to implement a comparer for it "public int Compare(EItem x, EItem y)" . The problem was that some of the properties of my object (EItem) were lists as well. As you can imagine comparing 2 lists is a little bit triky, so I have made an assumpition that I will compare only forst items x.ListProp[0] and y.ListProp[0] and I have used reflection in order to achieve that.  I attache sample code below, may be it will give you some new ideas:

    public class EItemComparer: IComparer<EItem>
        private string _propertyToCompare;
        public EItemComparer(string propertyToCompare)
            _propertyToCompare = propertyToCompare;
        public int Compare(EItem x, EItem y)
            //Type itemType = x.GetType();
            PropertyInfo eItemPropertyToCompareInfo  = typeof(EItem).GetProperty(_propertyToCompare);
            if (eItemPropertyToCompareInfo != null)
                object eItemXPropertyToCompareValue = eItemPropertyToCompareInfo.GetValue(x, null);
                object eItemYPropertyToCompareValue = eItemPropertyToCompareInfo.GetValue(y, null);
                //if property is a Generic.List type we can not just copare collections 
                //instead first element value will be picked and compared
                //if (eItemXPropertyToCompareValue.GetType().ToString().Contains("Generic.List"))
                if (eItemXPropertyToCompareValue is IList)
                    //get Generic.List "Count" property info
                    PropertyInfo listPropertyCountPropertyInfo = eItemXPropertyToCompareValue.GetType().GetProperty("Count");
                    //if List has elements, set eItemXPropertyToCompareValue and eItemYPropertyToCompareValue to the 
                    //first element in the List
                    int eItemXListPropertyCountPropertyValue = (int)listPropertyCountPropertyInfo.GetValue(eItemXPropertyToCompareValue, null);
                    eItemXPropertyToCompareValue = eItemXListPropertyCountPropertyValue > 0 ? ((IList)eItemXPropertyToCompareValue)[0] : null;
                    int eItemYListPropertyCountPropertyValue = (int)listPropertyCountPropertyInfo.GetValue(eItemYPropertyToCompareValue, null);
                    eItemYPropertyToCompareValue = eItemYListPropertyCountPropertyValue > 0 ? ((IList)eItemYPropertyToCompareValue)[0] : null;
                return Comparer.DefaultInvariant.Compare(eItemXPropertyToCompareValue, eItemYPropertyToCompareValue);
                throw new Exception(_propertyToCompare + " is not a valid property to sort on.  It doesn't exist in the Class.");
//sort EItem List 
EItemComparer cmp = new EItemComparer(sortExpression);

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.