AFg*_*one 2 c# delegates object
我正在使用反射类来调用某些其他dll上的方法.其中一个方法的参数是委托的类型.
我想通过使用反射来调用这些方法.所以我需要将函数参数作为对象数组传递,但我找不到任何关于如何将委托转换为对象的内容.
提前致谢
委托是一个对象.只需像平常一样创建预期的委托,并在参数数组中传递它.这是一个相当人为的例子:
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);
}
Run Code Online (Sandbox Code Playgroud)