C# 从 MethodInfo 创建委托

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11120401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:34:18  来源:igfitidea点击:

Creating delegate from MethodInfo

c#delegatesattributesmethodinfo

提问by thecaptain0220

I am currently running into an issue trying to create delegates from MethodInfo. My overall goal is to look through the methods in a class and create delegates for ones marked with a certain attribute. I am trying to use CreateDelegatebut I am getting the following error.

我目前遇到了一个问题,试图从MethodInfo. 我的总体目标是查看类中的方法并为标有特定属性的方法创建委托。我正在尝试使用,CreateDelegate但出现以下错误。

Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

无法绑定到目标方法,因为其签名或安全透明度与委托类型的签名或安全透明度不兼容。

Here is my code

这是我的代码

public class TestClass
{
    public delegate void TestDelagate(string test);
    private List<TestDelagate> delagates = new List<TestDelagate>();

    public TestClass()
    {
        foreach (MethodInfo method in this.GetType().GetMethods())
        {
            if (TestAttribute.IsTest(method))
            {
                TestDelegate newDelegate = (TestDelagate)Delegate.CreateDelegate(typeof(TestDelagate), method);
                delegates.Add(newDelegate);
            }
        }
    }

    [Test]
    public void TestFunction(string test)
    {

    }
}

public class TestAttribute : Attribute
{
    public static bool IsTest(MemberInfo member)
    {
        bool isTestAttribute = false;

        foreach (object attribute in member.GetCustomAttributes(true))
        {
            if (attribute is TestAttribute)
                isTestAttribute = true;
        }

        return isTestAttribute;
    }
}

采纳答案by Jon Skeet

You're trying to create a delegate from an instancemethod, but you're not passing in a target.

您正在尝试从实例方法创建委托,但没有传入目标。

You could use:

你可以使用:

Delegate.CreateDelegate(typeof(TestDelagate), this, method);

... or you could make your method static.

...或者你可以使你的方法静态。

(If you need to cope with both kinds of method, you'll need to do that conditionally, or pass in nullas the middle argument.)

(如果您需要处理这两种方法,则需要有条件地执行此操作,或者null作为中间参数传入。)

回答by lidqy

You need a different signature for the delegate, if it has no target. The target needs to be passed as the first argument then

如果委托没有目标,则您需要不同的签名。目标需要作为第一个参数传递然后

public class TestClass
{
    public delegate void TestDelagate(TestClass instance, string test);
    private List<TestDelagate> delagates = new List<TestDelagate>();

    public TestClass()
    {
        foreach (MethodInfo method in this.GetType().GetMethods())
        {
            if (TestAttribute.IsTest(method))
            {
                TestDelegate newDelegate = (TestDelagate)Delegate.CreateDelegate(typeof(TestDelagate), null, method);
                delegates.Add(newDelegate);
                //Invocation:
                newDelegate.DynamicInvoke(this, "hello");

            }
        }
    }

    [Test]
    public void TestFunction(string test)
    {

    }
}