如何确定一个方法是否是泛型方法的泛型实例
我有一个 MethodInfo 传递给一个函数,我想执行以下操作
MethodInfo containsMethod = typeof(ICollection<>).GetMethod("Contains");
if (methodInfo.Equals(containsMethod)
{
// do something
}
但这不起作用,因为 methodInfo 具有特定的泛型类型。如果我知道 ICollection 始终是字符串类型,则该示例确实有效。
MethodInfo containsMethod = typeof(ICollection<string>).GetMethod("Contains");
if (methodInfo.Equals(containsMethod)
{
// do something
}
如何检查 MethodInfo 是否是泛型方法的任何类型实例,而不关心类型是什么?
谢谢。
编辑:问题澄清
正如正确指出的那样,该方法不是通用的,但包含的类是通用的,所以问题更多的是如何找出 MethodInfo 是否适用于 ICollection<> 的类型实例。
编辑:更多上下文
我正在编写一个 Linq 提供程序并尝试处理“in”情况
IList<string> myList = new List<string>{ "1", "2" };
from Something s in ...
where myList.Contains(s.name)
select s;
I have a MethodInfo passed in to a function and I want to do the following
MethodInfo containsMethod = typeof(ICollection<>).GetMethod("Contains");
if (methodInfo.Equals(containsMethod)
{
// do something
}
But this doesn't work because the methodInfo has a specific generic type. For the example does work if I knew that the ICollection was always of type string.
MethodInfo containsMethod = typeof(ICollection<string>).GetMethod("Contains");
if (methodInfo.Equals(containsMethod)
{
// do something
}
How can I check whether the MethodInfo is a ANY typed instance of the generic method without caring what the type is?
Thanks.
EDIT: Question clarification
As correctly pointed out the method is not generic but the containing class is so the question is more how to I find out if the MethodInfo is for a Type which is a typed instance of ICollection<>.
EDIT: more context
I am writing a Linq provider and trying to handle the "in" case
IList<string> myList = new List<string>{ "1", "2" };
from Something s in ...
where myList.Contains(s.name)
select s;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请注意,
ICollection.Contains
不是泛型方法 - 它是泛型类型的非泛型方法。否则,IsGenericMethod
和GetGenericTypeDefinition
会有所帮助。您可以获取泛型类型定义 (DeclaringType.GetGenericTypeDefinition()
) 并返回到Contains
,但我想知道您是否正在以困难的方式解决这个问题。通常,如果您使用反射,则放弃到非通用
IList
可能是务实的 - 除非您需要类型数据(例如,用于元编程)。在这种情况下,我会考虑仔细查看是否可以简化此处的设置。Note that
ICollection<T>.Contains
is not a generic method - it is a non-generic method of a generic type. OtherwiseIsGenericMethod
andGetGenericTypeDefinition
would help. You could obtain the generic type definition (DeclaringType.GetGenericTypeDefinition()
) and work back up toContains
, but I wonder if you are approaching this problem the hard way.Usually, if you are using reflection, it may be pragmatic to drop to non-generic
IList
- unless you need the type data (for example, for meta-programming). And in that case, I would consider looking closely to see if you can simplify the setup here.您可以检查声明类型:
You could check the declaring type:
需要添加一些错误检查,但我相信这大致可以满足您的要求。您可以使用带或不带类型参数的方法作为参数。
Some error checking would need to be added to this, but I believe this roughly does what you want. You can use a method with or without a type argument as the parameter.
问题是您没有泛型方法:您在泛型类型上有非泛型方法。我不知道如何使用反射直接从开放泛型类型上的方法定义转到封闭泛型类型上的相同方法,反之亦然。但是,您可以利用以下事实:开放泛型类型和封闭泛型类型上的
GetMethods()
返回的方法应始终采用相同的顺序,并按索引进行转换:The problem is that you don't have a generic method: you have a non-generic method on a generic type. I don't know of a way to use reflection to go directly from a method definition on an open generic type to that same method on a closed generic type or vice versa. However, you can take advantage of the fact that the methods returned by
GetMethods()
on the open and closed generic types should always be in the same order and do the translation by index:尝试这个方法
,这里是完整的示例用法。
try this method
and here is the full example usage.