如何在 C# 中将委托转换为对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1090974/
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-06 07:59:54  来源:igfitidea点击:

How to convert delegate to object in C#?

c#objectdelegates

提问by AFgone

I am using reflection class to invoke some methods which are on the some other dll. And one of the methods' parameters are type of delegate.

我正在使用反射类来调用其他一些 dll 上的一些方法。方法的参数之一是委托类型。

And I want to invoke this methods by using reflection. So I need to pass function parameters as object array, but I could not find anything about how to convert delegate to object.

我想通过使用反射来调用这个方法。所以我需要将函数参数作为对象数组传递,但我找不到任何关于如何将委托转换为对象的信息。

Thanks in advance

提前致谢

采纳答案by Matt Howells

A delegate is an object. Just create the expected delegate as you would normally, and pass it in the parameters array. Here is a rather contrived example:

委托是一个对象。只需像往常一样创建预期的委托,并将其传递到参数数组中。这是一个相当人为的例子:

class Mathematician {
    public delegate int MathMethod(int a, int b);

    public int DoMaths(int a, int b, MathMethod mathMethod) {
        return mathMethod(a, b);
    }
}

[Test]
public void Test() {
    var math = new Mathematician();
    Mathematician.MathMethod addition = (a, b) => a + b;
    var method = typeof(Mathematician).GetMethod("DoMaths");
    var result = method.Invoke(math, new object[] { 1, 2, addition });
    Assert.AreEqual(3, result);
}

回答by Cédric Rup

Instances of delegates are objects, so this code works (C#3 style) :

委托的实例是对象,因此此代码有效(C#3 风格):

Predicate<int> p = (i)=> i >= 42;

Object[] arrayOfObject = new object[] { p };

Hope it helps !

希望能帮助到你 !

Cédric

塞德里克

回答by Darin Dimitrov

Here's an example:

下面是一个例子:

class Program
{
    public delegate void TestDel();

    public static void ToInvoke(TestDel testDel)
    {
        testDel();
    }

    public static void Test()
    {
        Console.WriteLine("hello world");
    }

    static void Main(string[] args)
    {
        TestDel testDel = Program.Test;
        typeof(Program).InvokeMember(
            "ToInvoke", 
            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
            null,
            null,
            new object[] { testDel });
    }
}

回答by CraigTP

I think this blog post:

我认为这篇博文:

C# Reflection - Dealing with Remote Objects

C# 反射 - 处理远程对象

answers your question perfectly.

完美回答你的问题。

回答by AndersK

you can see a delegate as variable type "function". the delegate describes the parameters and return value for a matching function.

您可以将委托视为变量类型“函数”。委托描述了匹配函数的参数和返回值。

delegate void Foo(int a);  // here a new delegate obj type Foo has been declared

the above example allows 'Foo' to be used as a data type, the only allowed object that can be matched with a variable of type Foo data type is a method with the same signature so:

上面的示例允许将 'Foo' 用作数据类型,唯一可以与 Foo 数据类型类型的变量匹配的对象是具有相同签名的方法,因此:

void MyFunction(int x);    

Foo D = MyFunction; // this is OK

void MyOtherFunction(string x);

Foo D = MyOtherFunction; // will yield an error since not same signature.

Once you have assigned a method to a delegate, you can invoke the method via the delegate:

将方法分配给委托后,您可以通过委托调用该方法:

int n = 1;
D( n );      // or D.Invoke( n );