我試圖檢測一個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