locked
Simple reflection with IEnumerable RRS feed

  • Question

  • Hi,

    I need to determine what inner class a IEnumerable contains.

    The IEnumerable could be a List<MyComplexClass>, Dictionary<int,MyComplexClass> and so on. I need to get the inner Type, int this case MyComplexClass.

    Pleas note, the IEnumerable will contain diffrent classes and not always a class called MyComplexClass.

    This needs to be done on Type, no objects will exists at this point.

    BestRegards

    Wednesday, March 31, 2010 8:09 AM

Answers

  • You can use the GetGenericArguments method. If you're not sure the type being analyzed is a generic type, you should verify it with the property IsGenericType before calling the GetGenericArguments method

     

    Type t = typeof(List<decimal>);

    if(t.IsGenericType)

    return t.GetGenericArguments()[0];

     


    -- Blog: http://geeklyeverafter.blogspot.com/
    • Marked as answer by SnowJim Wednesday, March 31, 2010 9:48 AM
    Wednesday, March 31, 2010 9:06 AM

All replies

  • You can use the GetGenericArguments method. If you're not sure the type being analyzed is a generic type, you should verify it with the property IsGenericType before calling the GetGenericArguments method

     

    Type t = typeof(List<decimal>);

    if(t.IsGenericType)

    return t.GetGenericArguments()[0];

     


    -- Blog: http://geeklyeverafter.blogspot.com/
    • Marked as answer by SnowJim Wednesday, March 31, 2010 9:48 AM
    Wednesday, March 31, 2010 9:06 AM
  • Thanks! That works grate!
    Wednesday, March 31, 2010 9:48 AM