2016-11-30 20 views
0

我有一個類,它具有從特定函數(sin(x))獲取值的方法和從任何使用代理的函數獲取值的方法。如何使用代理作爲函數中的參數使用反射

namespace ValueFunctionFinder { 

public delegate double SomeFunction(double arg); 

public class ValueFunctionFinderClass 
{ 
    public double GetValue(double x) 
    { 
     double y = Math.Sin(x); 
     return y; 
    } 

    public double GetValueDel(double x, SomeFunction function) 
    { 
     double y = function(x); 
     return y; 
    } 

} 

我使用這個類在我的主:

static void Main(string[] args) 
{ 
    ValueFunctionFinderClass finder = new ValueFunctionFinderClass(); 

    double x = Math.Sin(Math.PI/6); 
    // find value from specific function 
    double y = finder.GetValue(x); 
    Console.WriteLine($"Sin(PI/6) = {y}"); 

    // find value from any function 
    SomeFunction function = Math.Sin; 
    y = finder.GetValueDel(x, function); 
    Console.WriteLine($"Sin(PI/6) = {y}"); 

    Console.ReadLine(); 
} 

在另一個項目中,我想與反思再次使用它:

static void Main(string[] args) 
{ 
    Assembly assembly = Assembly.Load("ValueFunctionFinder"); 
    Type functionFinderType = assembly.GetType("ValueFunctionFinder.ValueFunctionFinderClass"); 
    object functionFinderObj = Activator.CreateInstance(functionFinderType); 

    // find value from specific function using Reflection 
    MethodInfo getValueMethodInfo = functionFinderType.GetMethod("GetValue"); 
    double x = Math.Sin(Math.PI/6); 
    object y = getValueMethodInfo.Invoke(functionFinderObj, new object[] { x }); 
    Console.WriteLine($"Sin(PI/6) = {y}"); // it works OK 

    // find value from any function with Reflection 
    Type someFunctionType = assembly.GetType("ValueFunctionFinder.SomeFunction"); 

    // I should use smth like this: 
    // ********************************************** 
    // dynamic creation of delegate 
    //Delegate del = Delegate.CreateDelegate(someFunctionType, someMethodInfo); // what kind of methodInfo shoul I use? 
    // dynamic use of delegate 
    //object function = del.DynamicInvoke(arguments); // what kind of arguments? Math.Sin? 
    // ********************************************** 
    MethodInfo getValueDelMethodInfo = functionFinderType.GetMethod("GetValueDel"); 
    //y = getValueDelMethodInfo.Invoke(functionFinderObj, new object[] {x, function}); 
    Console.WriteLine($"Sin(PI/6) = {y}"); // how do this? 
    Console.ReadLine(); 
} 

我已閱讀MSDN這個資源,但coudn't瞭解如何使用委託作爲函數的參數,使用反射。

+0

爲什麼你需要你自己的方法做什麼,但調用的委託?只需直接調用委託。 – Servy

回答

0
SomeFunction function = Math.Sin; 

SomeFunction function = new SomeFunction(Math.Sin); 

和反思的快捷方式可以爲我們做這與MethodInfo.CreateDelegate method

var getValueDelMethod = functionFinderType.GetMethod("GetValueDel"); 

// create a delegate to the target method of the desired type 
var delegateType = typeof(SomeFunction); 
var sinMethod = typeof(Math).GetMethod(nameof(Math.Sin)); 
var delegateObj = sinMethod.CreateDelegate(delegateType); 

var result = getValueDelMethod.Invoke(functionFinderObj, new object[] {x, delegateObj}); 
+0

太棒了!這是我需要的!謝謝你,thehennyy。 –

相關問題