比较泛型:“主类型” IEnumerable的<>它是通用的,但匹配所有特定类型(IEnumerable< int&gt ;, IEnumerable

编程入门 行业动态 更新时间:2024-10-13 10:25:14
本文介绍了比较泛型:“主类型” IEnumerable的<>它是通用的,但匹配所有特定类型(IEnumerable< int&gt ;, IEnumerable< object&gt ;, ...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想比较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<>,因为稍后我会进一步反思并过滤掉所有转换为 $ 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的&LT;&GT;它是通用的,但匹配所有特定类型(IEnumerable&lt; int&a

本文发布于:2023-11-09 04:22:18,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   它是   LT   amp   GT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!