Link to home
Start Free TrialLog in
Avatar of Valimai
Valimai

asked on

.Net 2.0+ Reflect Generic List Type

hi there,

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

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

How can I get MyClass into "Type t" via the "list" variable?
ASKER CERTIFIED SOLUTION
Avatar of hamidovt
hamidovt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Valimai
Valimai

ASKER

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.
Avatar of Valimai

ASKER

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);
            }
            else
            {
                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);
eItems.Sort(cmp);

Open in new window