.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?
hamidovtConnect With a Mentor Commented:
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???
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

All Courses

From novice to tech pro — start learning today.