我试图检测一个Type对象的特定实例是一个通用的“IEnumerable的” ....NET反思:检测的IEnumerable <T>
我能想出的最好的是:
// theType might be typeof(IEnumerable<string>) for example... or it might not
bool isGenericEnumerable = theType.GetGenericTypeDefinition() == typeof(IEnumerable<object>).GetGenericTypeDefinition()
if(isGenericEnumerable)
{
Type enumType = theType.GetGenericArguments()[0];
etc. ...// enumType is now typeof(string)
但是,这似乎有点间接 - 是否有一个更直接/优雅的方式来做到这一点?
请参阅我的跟进:http://stackoverflow.com/questions/1650310/net-reflection-determining-whether-an-array-of-t-would-be-convertible-to-some-o – 2009-10-30 14:57:35