从方法参考C#得到的MethodInfo

编程入门 行业动态 更新时间:2024-10-26 12:30:10
本文介绍了从方法参考C#得到的MethodInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们可以使用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公布答案,但这个博客帖子的轻微调整似乎达到你的要求了; blog.functionalfun/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; blog.functionalfun/2009/10/getting-methodinfo-of-generic-method.html

样品的使用情况如下;

Sample usage would be as follows;

var methodInfo = SymbolExtensions.GetMethodInfo(() => Program.Main());

原来的答复是,这个问题; stackoverflow/a/9132588/5827

更多推荐

从方法参考C#得到的MethodInfo

本文发布于:2023-11-05 04:18:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   MethodInfo

发布评论

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

>www.elefans.com

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