简体   繁体   English

如何使用方法名称获取非静态MethodInfo(不在字符串中,不在类类型中搜索)

[英]How to get non-static MethodInfo using method name (not in string, not searching in class type)

Consider following code: 考虑以下代码:

public class Test
{
    [System.AttributeUsage(System.AttributeTargets.Method)]
    class MethodAttribute : System.Attribute
    {
        public MethodAttribute(System.Reflection.MethodInfo methodInfo)
        {

        }
    }

    public void Foo()
    {

    }

    [Method(Test.Foo)] //< THIS IS THE IMPORTANT LINE
    public void Boo()
    {

    }
}

I want to store MethodInfo instance of Foo in attribute of Boo, but problem is, that I cannot use Foo nor Test.Foo to get instance of MethodInfo . 我想将Foo的MethodInfo实例存储在Boo的属性中,但是问题是,我无法使用FooTest.Foo来获取MethodInfo实例。 I can NOT use typeof(Test).GetMethod("Foo") (not my decision). 不能使用typeof(Test).GetMethod("Foo") (不是我的决定)。

Important information: 重要信息:

Absolutely unimportant information: 绝对不重要的信息:

  • Why I cannot use typeof(Test).GetMethod("Foo") : I'm not allowed. 为什么我不能使用typeof(Test).GetMethod("Foo") :我不允许。 It's not in my power to change this decision. 我没有权力更改此决定。 I can not . 不能 (Also, personally, I would like to avoid it anyway as it needs to do some lookup instead of getting statically the data. Also it won't be changed automatically during refactoring and will be checking run-time, not compile-time.) (另外,就我个人而言,我还是想避免它,因为它需要进行一些查找而不是静态获取数据。而且在重构期间它不会自动更改,并且会检查运行时而不是编译时。)
  • Why I want to do this: later in code, I want to create a delegate of Boo() (this time normally, with an instance) and use in special even system or something. 为什么要执行此操作:稍后在代码中,我想创建Boo()的委托(这次通常是使用实例),并在特殊的甚至系统中使用它。 Before it's called, this attribute allows to setup method to be called in prepare for main event (it's optional, it can be null) and I know how to create a delegate when I have an object and a method info. 在调用之前,此属性允许在准备主事件时调用设置方法(它是可选的,它可以为null),并且我知道当我有一个对象和方法信息时如何创建委托。
  • Why I cannot just provide both delegates when registering or something: because class containing these methods is not always the one who registers to event source, I need to keep the registration code as simple as possible. 为什么我不能在注册时或在某些事情上同时提供两个委托:为什么包含这些方法的类并不总是向事件源注册的类,所以我需要使注册代码尽可能简单。 In other words, I want to avoid situation when person writing method responsible for connecting forgots to add this preparation delegate. 换句话说,我想避免负责连接的人编写方法忘记添加此准备委托人的情况。

use expression : 使用表达式:

static public class Metadata<T>
{
    static public PropertyInfo Property<TProperty>(Expression<Func<T, TProperty>> property)
    {
        var expression = property.Body as MemberExpression;
        return expression.Member as PropertyInfo;
    }
}

var foo = Metadata<Test>.Property(test => test.Foo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用MethodInfo调用非静态的lambda方法(使用&lt;&gt; c__DisplayClass1) - How to invoke lambda method that is non-static using MethodInfo (uses <>c__DisplayClass1) 获取IEnumerable的非静态MethodInfo <T> .First()(或使静态方法与EF一起使用) - Get Non-Static MethodInfo for IEnumerable<T>.First() (Or make the static method work with EF) 在编译时获取非静态方法的 MethodInfo - Getting MethodInfo at compile time for a non-static method 在非静态类的静态方法中抽象出类型 - Abstract away the type in a static method in a non-static class 一个非静态类如何调用另一个非静态类的方法? - How can a non-static class call another non-static class's method? 静态类中的匿名方法是非静态的? 如何调用它? - Anonymous method in static class is non-static? How to invoke it? C#获取类的非静态属性的名称 - C# Get the name of a non-static property of a class 伪造一个非静态 class 的 static 方法 - Fake a static method of a non-static class 实现了类方法的MethodInfo,如何获取接口方法的MethodInfo? - How to get MethodInfo of interface method, having implementing MethodInfo of class method? 如何仅对一个班级的3个孩子实施一种非静态方法? - How to implement one non-static method for only 3 children of a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM