如何获取 Array.IndexOf<string>(string[], string) MethodInfo? [英] How to Get Array.IndexOf<string>(string[], string) MethodInfo?
本文介绍了如何获取 Array.IndexOf<string>(string[], string) MethodInfo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取Array.IndexOf
的MethodInfo
?
我尝试使用此代码,但不起作用.
I try using this code, but doesn't work.
typeof(Array).GetMethod("IndexOf",
BindingFlags.Public | BindingFlags.Static, null,
new Type[] { typeof(string[]), typeof(string) }, null);
推荐答案
使用 BindingFlags.Public |BindingFlags.Static
下面的评论是正确的,问题在于IndexOf
方法是通用的——只有一个Array.IndexOf
.为了得到那个,这对我有用:
The comment below is correct, the problem is that the IndexOf
method is generic - there is only a Array.IndexOf<T>(T[], T)
. To get that one this is what worked for me:
var indexOfGeneric = typeof(Array).GetMethods(BindingFlags.Public | BindingFlags.Static)
.First(m => m.Name == "IndexOf"
&& m.GetParameters().Length == 2
&& m.IsGenericMethod );
这篇关于如何获取 Array.IndexOf<string>(string[], string) MethodInfo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文