比较泛型:“主类型” IEnumerable的<>它是通用的,但匹配所有特定类型(IEnumerable< int> ;, IEnumerable< object> ;, ...)? [英] Comparing generics: A "master-type" IEnumerable<> that is generic, but matches all specific types (IEnumerable<int>, IEnumerable<object>, ...)?
问题描述
我想比较typeof(IEnumerable<>)与IEnumerable的各种特定类的类型,例如
比较(typeof(IEnumerable<>),typeof(IEnumerable< object>))//应该返回true
比较(typeof(IEnumerable<>),typeof(IEnumerable< bof b比较(typeof(IEnumerable<>),typeof(IEnumerable< MyClass>))//应该返回true
比较(typeof(IEnumerable<>),typeof(IEnumerable))//应该返回FALSE因为IEnumerable不是通用的IEnumerable<>键入
我该怎么做?所有常见的方法如==或IsAssignableFrom对于上面的所有例子都返回false。
可能不需要这个问题,但一些背景:
我正在写一个转换类,将对象转换为其他类型。我使用的是属性(XlConverts):
public class XlConvertsAttribute:属性
{
public Type convert ;
public输入;
}
来标记每种方法转换成哪种类型。我的一个转换方法将一个对象转换为IEnumerable:
[XlConverts(converted = typeof(object),to = typeof(IEnumerable< ;>))]
public static IEnumerable< T> ToIEnumerable< T>(对象输入)
{
// ....
}
然后我有一个更一般的方法
public static object Convert(object input,Type toType)
{
// ...
}
以获得具有XlConverts.to == toType的方法,所以基本上它反映了它自己的类,以找到给定所需目标类型的approrpaite转换方法。
现在,当我调用Convert(input,typeof(IEnumerable)),它应该通过反射找到方法ToIEnumerable。但是因为我只能用[XlConverts(to = typeof(IEnumerable<>))标记它,而IEnumerable<>不是IEnumerable,所以它不会找到这个方法。<我知道只是使用IEnumerable而不是IEnumerable<>会在这里完成这项工作,但是我明确地需要使用泛型IEnumerable<>,因为稍后我会进一步反思并过滤掉所有转换为
$ b
谢谢!
public static bool Compare(Type genericType,Type t)
{
return t.IsGenericType&& t.GetGenericTypeDefinition()== genericType;
}
I want to to compare typeof(IEnumerable<>) to the types of various specific classes of IEnumerable, e.g.
Compare(typeof(IEnumerable<>), typeof(IEnumerable<object>)) // should return true
Compare(typeof(IEnumerable<>), typeof(IEnumerable<int>)) // should return true
Compare(typeof(IEnumerable<>), typeof(IEnumerable<MyClass>)) // should return true
Compare(typeof(IEnumerable<>), typeof(IEnumerable)) // should return FALSE because IEnumerable is not the generic IEnumerable<> type
How can I do this? All common methods such as == or IsAssignableFrom return false for all above examples.
Probably not necessary for the question, but some background:
I'm writing a conversion class that convers an object into some other type. I'm using Attributes (XlConverts):
public class XlConvertsAttribute : Attribute
{
public Type converts;
public Type to;
}
to flag which type each methods converts into. One of my conversion methods converts an object into IEnumerable:
[XlConverts(converts = typeof(object), to = typeof(IEnumerable<>))]
public static IEnumerable<T> ToIEnumerable<T>(object input)
{
// ....
}
Then I have a more general method
public static object Convert(object input, Type toType)
{
// ...
}
Which uses reflection to get the method that has XlConverts.to == toType, so basically it reflects its own class to find the approrpaite conversion method given the desired target type.
now when I call Convert(input, typeof(IEnumerable)), it is supposed to find the method ToIEnumerable by reflection. But as I can only flag it with [XlConverts(to = typeof(IEnumerable<>)), and IEnumerable<> is not IEnumerable, it won't find this method.
I'm aware just using IEnumerable instead of IEnumerable<> would do the job here, but I explicitly need to use the generic IEnumerable<> because later on, I want to do further reflection and filter out all methods that convert into a generic type.
Thanks!
public static bool Compare(Type genericType, Type t)
{
return t.IsGenericType && t.GetGenericTypeDefinition() == genericType;
}
这篇关于比较泛型:“主类型” IEnumerable的<>它是通用的,但匹配所有特定类型(IEnumerable< int> ;, IEnumerable< object> ;, ...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!