Get type of elements in a List derived from List<T> in C#

大憨熊 提交于 2021-01-29 01:58:12

问题


Lets say that I have classes which derive from List<T>:

public class StringList : List<String> {}
public class NameList : StringList {}

public class IntList : List<int> {}

Now I have a generic method which expects type List<T>:

public static Method<T>() { ... }

How can I determine the type of elements contained in a list in this method, i.e. how to get the generic argument type in a derived class?

For base class I can call typeof(T>.GetGenericArguments(), but for derived class it returns zero size.

PS: In my concrete situation the type which method expects is not exactly List<T>, but IList.


回答1:


You can write the method like this:

public static void Method<T>(List<T> thing) (or IList<T>)
{
    //Here, `T` is the type of the elements in the list
}

Of if you need a reflection-based check:

public static void Method(Type myType) 
{
    var thing = myType.GetInterfaces()
        .Where(i => i.IsGenericType)
        .Where(i => i.GetGenericTypeDefinition() == typeof(IList<>))
        .FirstOrDefault()
        .GetGenericArguments()[0];
}

Note that you'll need appropriate sanity checks here (rather than FirstOrDefault() and 0 indexing)




回答2:


If you want both the type of the list and the element type of the list at compile time, your Method must have two generic definitions like this:

public static void Method<T, E>(T list) where T : List<E> 
{ 
    // example1
    // T is List<int> and E is int

    // example2
    // T is NameList and E is String
}

Method<List<int>, int>(new List<int>());   //example1
Method<NameList, string>(new NameList());  //example2


来源:https://stackoverflow.com/questions/41234210/get-type-of-elements-in-a-list-derived-from-listt-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!