从方法参考C#得到的MethodInfo [英] get methodinfo from a method reference C#
问题描述
我们可以使用C#的typeof
关键字,当我们想要得到的特定类型的类型实例。但我可以使用,如果我想获得的MethodInfo
的方法通过它的参考?
We can use a C# typeof
keyword when we want to get Type instance for specified type. But what can I use if I want to get MethodInfo
of a method by it's reference?
例如我有一个简单的控制台应用程序。它包含 Program.Main
方法。我想获得的MethodInfo
通过使用类似 methodinfoof(Program.Main)
。我有这个问题,因为该方法的名称可能会改变,所以我不能只用 Type.GetMethodInfo(字符串方法名)
为
For example I have a simple console app. It contains Program.Main
method. I want to get MethodInfo
by using something like methodinfoof(Program.Main)
. I have this problem because the method names might change, so I cannot just use Type.GetMethodInfo(string MethodName)
for that.
我有大约10 000的方法,我想获得的MethodInfo
,因此添加任何自定义属性或其他任何以我的方法是不是一个解决方案。
I have about 10 000 methods for which I would like to get MethodInfo
, so adding any custom attributes or anything else to my methods is not a solution.
推荐答案
一previously公布答案,但这个博客帖子的轻微调整似乎达到你的要求了; <一href="http://blog.functionalfun.net/2009/10/getting-methodinfo-of-generic-method.html">http://blog.functionalfun.net/2009/10/getting-methodinfo-of-generic-method.html
Slight adaptation of a previously posted answer, but this blog post seems to achieve what you're asking for; http://blog.functionalfun.net/2009/10/getting-methodinfo-of-generic-method.html
样品的使用情况如下;
Sample usage would be as follows;
var methodInfo = SymbolExtensions.GetMethodInfo(() => Program.Main());
原来的答复是,这个问题; http://stackoverflow.com/a/9132588/5827
这篇关于从方法参考C#得到的MethodInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!