如何判断某物是否为 IEnumerable<>?
How to tell if something is an IEnumerable<>?
我有一个类型。
如何判断它是否是 IEnumerable<>
?
这些:
typeof(IEnumerable<>).IsAssignableFrom(memberType);
typeof(IEnumerable<object>).IsAssignableFrom(memberType);
return IEnumerable<int>
为假
而这个:
typeof(IEnumerable).IsAssignableFrom(memberType);
return对于字符串为真。
嗯...我找到了 方法...
private static bool IsGenericEnumerable(this [NotNull] Type type) =>
typeof(IEnumerable<>).IsAssignableFrom(type)
|| typeof(IEnumerable<object>).IsAssignableFrom(type)
|| (typeof(IEnumerable<char>).IsAssignableFrom(type) && type != typeof(string))
|| typeof(IEnumerable<byte>).IsAssignableFrom(type)
|| typeof(IEnumerable<sbyte>).IsAssignableFrom(type)
|| typeof(IEnumerable<ushort>).IsAssignableFrom(type)
|| typeof(IEnumerable<short>).IsAssignableFrom(type)
|| typeof(IEnumerable<uint>).IsAssignableFrom(type)
|| typeof(IEnumerable<int>).IsAssignableFrom(type)
|| typeof(IEnumerable<ulong>).IsAssignableFrom(type)
|| typeof(IEnumerable<long>).IsAssignableFrom(type)
|| typeof(IEnumerable<float>).IsAssignableFrom(type)
|| typeof(IEnumerable<double>).IsAssignableFrom(type)
|| typeof(IEnumerable<decimal>).IsAssignableFrom(type)
|| typeof(IEnumerable<DateTime>).IsAssignableFrom(type);
...但这有点可怕,我希望有更好的方法。
反思很有趣;旁注:请记住,您可以在同一类型上实现 IEnumerable<X>
和 IEnumerable<Y>
(等),因此为了简单起见,我只报告第一个发现任意:
static void Main()
{
Console.WriteLine(FindFirstIEnumerable(typeof(int))); // null
Console.WriteLine(FindFirstIEnumerable(typeof(string))); // System.Char
Console.WriteLine(FindFirstIEnumerable(typeof(Guid[]))); // System.Guid
Console.WriteLine(FindFirstIEnumerable(typeof(IEnumerable<float>))); // System.Single
}
static Type FindFirstIEnumerable(Type type)
{
if (type == null || !typeof(IEnumerable).IsAssignableFrom(type))
return null; // anything IEnumerable<T> *must* be IEnumerable
if (type.IsInterface && type.IsGenericType
&& type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
return type.GetGenericArguments()[0];
}
foreach(var iType in type.GetInterfaces())
{
if (iType.IsGenericType &&
iType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
return iType.GetGenericArguments()[0];
}
}
return null;
}
我有一个类型。
如何判断它是否是 IEnumerable<>
?
这些:
typeof(IEnumerable<>).IsAssignableFrom(memberType);
typeof(IEnumerable<object>).IsAssignableFrom(memberType);
return IEnumerable<int>
而这个:
typeof(IEnumerable).IsAssignableFrom(memberType);
return对于字符串为真。
嗯...我找到了 方法...
private static bool IsGenericEnumerable(this [NotNull] Type type) =>
typeof(IEnumerable<>).IsAssignableFrom(type)
|| typeof(IEnumerable<object>).IsAssignableFrom(type)
|| (typeof(IEnumerable<char>).IsAssignableFrom(type) && type != typeof(string))
|| typeof(IEnumerable<byte>).IsAssignableFrom(type)
|| typeof(IEnumerable<sbyte>).IsAssignableFrom(type)
|| typeof(IEnumerable<ushort>).IsAssignableFrom(type)
|| typeof(IEnumerable<short>).IsAssignableFrom(type)
|| typeof(IEnumerable<uint>).IsAssignableFrom(type)
|| typeof(IEnumerable<int>).IsAssignableFrom(type)
|| typeof(IEnumerable<ulong>).IsAssignableFrom(type)
|| typeof(IEnumerable<long>).IsAssignableFrom(type)
|| typeof(IEnumerable<float>).IsAssignableFrom(type)
|| typeof(IEnumerable<double>).IsAssignableFrom(type)
|| typeof(IEnumerable<decimal>).IsAssignableFrom(type)
|| typeof(IEnumerable<DateTime>).IsAssignableFrom(type);
...但这有点可怕,我希望有更好的方法。
反思很有趣;旁注:请记住,您可以在同一类型上实现 IEnumerable<X>
和 IEnumerable<Y>
(等),因此为了简单起见,我只报告第一个发现任意:
static void Main()
{
Console.WriteLine(FindFirstIEnumerable(typeof(int))); // null
Console.WriteLine(FindFirstIEnumerable(typeof(string))); // System.Char
Console.WriteLine(FindFirstIEnumerable(typeof(Guid[]))); // System.Guid
Console.WriteLine(FindFirstIEnumerable(typeof(IEnumerable<float>))); // System.Single
}
static Type FindFirstIEnumerable(Type type)
{
if (type == null || !typeof(IEnumerable).IsAssignableFrom(type))
return null; // anything IEnumerable<T> *must* be IEnumerable
if (type.IsInterface && type.IsGenericType
&& type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
return type.GetGenericArguments()[0];
}
foreach(var iType in type.GetInterfaces())
{
if (iType.IsGenericType &&
iType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
return iType.GetGenericArguments()[0];
}
}
return null;
}