如何确定未知类型是否为 List<T> 以及 T 是什么类型
How to determine if an unknown type is a List<T> and what type T is
我正在尝试提取未知 属性 的类型并确定它是否是 List<T>
,以及 T
是否属于特定类型。
我知道如何使用反射来确定 属性 的类型,但是我不知道如何确定 属性 是否是 List<T>
以及什么类型 T
是。希望大家帮帮忙。
编辑:How to get the type of T from a member of a generic class or method? 不太适用。那里的答案假设我知道我的未知类型首先是 List
。我已经编辑了我的标题以使其更清楚。
object o = new List<double>();
Type t = o.GetType();
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>))
Console.WriteLine("This is a list of type {0}", t.GenericTypeArguments[0].Name);
我正在尝试提取未知 属性 的类型并确定它是否是 List<T>
,以及 T
是否属于特定类型。
我知道如何使用反射来确定 属性 的类型,但是我不知道如何确定 属性 是否是 List<T>
以及什么类型 T
是。希望大家帮帮忙。
编辑:How to get the type of T from a member of a generic class or method? 不太适用。那里的答案假设我知道我的未知类型首先是 List
。我已经编辑了我的标题以使其更清楚。
object o = new List<double>();
Type t = o.GetType();
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>))
Console.WriteLine("This is a list of type {0}", t.GenericTypeArguments[0].Name);