我正在尝试提取未知属性的类型,并确定它是否为List<T>,以及T是否为特定类型。
我理解如何使用反射来确定属性的类型,但是我不知道如何确定属性是否是List<T>,以及T是什么类型。希望你们能帮忙。
编辑:How to get the type of T from a member of a generic class or method?并不完全适用。那里的答案假设我知道我的未知类型是一个List。我已经编辑了我的标题,使之更加清晰。
发布于 2015-11-12 09:38:10
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);https://stackoverflow.com/questions/33668005
复制相似问题