【问题标题】:How can I pass a method acquired by reflection in C# to a method that accepts the method as a delegate?如何将通过 C# 中的反射获取的方法传递给接受该方法作为委托的方法?
【发布时间】:2010-12-10 05:11:15
【问题描述】:

我意识到需要多次阅读标题才能理解... :)

我实现了一个自定义属性,该属性应用于我的类中的方法。 我应用属性的所有方法都具有相同的签名,因此我为它们定义了一个委托:

public delegate void TestMethod();

我有一个接受该委托作为参数的结构

struct TestMetaData
{
  TestMethod method;
  string testName;
}

是否可以从反射中获取具有自定义属性的方法并将其传递到结构中的“方法”成员?

我知道你可以调用它,但我认为反射不会为我提供我可以转换为 TestMethod 委托的类中的实际方法。

【问题讨论】:

    标签: c# reflection methods delegates attributes


    【解决方案1】:

    通过反射获得 MethodInfo 后,您可以使用 Delegate.CreateDelegate 将其转换为委托,然后使用反射将其直接设置为结构的属性/字段。

    【讨论】:

      【解决方案2】:

      您可以使用 Invoke 或 Delegate.CreateDelegate 创建一个在运行时调用反射方法的委托。

      例子:

      using System;
      class Program
      {
          public delegate void TestMethod();
          public class Test
          {
              public void MyMethod()
              {
                  Console.WriteLine("Test");
              }
          }
          static void Main(string[] args)
          {
              Test t = new Test();
              Type test = t.GetType();
              var reflectedMethod = test.GetMethod("MyMethod");
              TestMethod method = (TestMethod)Delegate.CreateDelegate(typeof(TestMethod), t, reflectedMethod);
              method();
          }
      }
      

      【讨论】:

      • Delegate.CreateDelegate 会更简单...那里没有调用。
      • @Marc,你说得对,我使用 CreateDelegate 更新了一个示例。
      【解决方案3】:

      您还需要已经声明合适的委托

      【讨论】:

        【解决方案4】:

        举个例子:

        using System;
        using System.Linq;
        
        using System.Reflection;
        public delegate void TestMethod();
        class FooAttribute : Attribute { }
        static class Program
        {
            static void Main() {
                // find by attribute
                MethodInfo method =
                    (from m in typeof(Program).GetMethods()
                     where Attribute.IsDefined(m, typeof(FooAttribute))
                     select m).First();
        
                TestMethod del = (TestMethod)Delegate.CreateDelegate(
                    typeof(TestMethod), method);
                TestMetaData tmd = new TestMetaData(del, method.Name);
                tmd.Bar();
            }
            [Foo]
            public static void TestImpl() {
                Console.WriteLine("hi");
            }
        }
        
        struct TestMetaData
        {
            public TestMetaData(TestMethod method, string name)
            {
                this.method = method;
                this.testName = name;
            }
            readonly TestMethod method;
            readonly string testName;
            public void Bar() { method(); }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多