c#:使用反射来动态调用显式实现的方法



我正在尝试使用反射动态调用显式实现的方法。我的方法如下:

public static double GetValueOfAverage(string typeName, string methodName, object[] arguments){
    // ... get the Type for the class
    Type calledType = Type.GetType(typeName, true);
    double avg  = (double)calledType.InvokeMember(
        methodName,
        BindingFlags.InvokeMethod | BindingFlags.Public | 
        BindingFlags.Static | BindingFlags.NonPublic,
        null,
        null,
        new object[] { arguments }
    );
    // Return the value returned by the called method.
    return avg;
}

我这样调用这个方法:

// ... calculate and set the slow moving average value
var slowArguments = new object[] { SlowPeriod, CurrentBar, Input, slowAverage};
movingAvg = GetValueOfAverage("MovingAverage", slowAvgMethod, slowArguments);

我的移动平均线类是这样实现的:

public static class MovingAverage{
    public static double _EMA(int Period, int CurrentBar, DataSeries Input, DataSeries average){
        double avg = CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * average[1];
        return avg;
    }
}

我得到以下错误:

无法从程序集'f9e1d550bd4d44ddbdc78abaffa07d31, Version=7.0.1000.22, Culture=neutral, PublicKeyToken=null'加载类型'MovingAverage' .

我试过将命名空间添加到类规范中,但这也不起作用。我所做的如下:

在GetValueOfAverage方法中,我插入了以下代码
var methodInfo = System.Reflection.MethodBase.GetCurrentMethod();
var fullName = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;
string str = methodInfo.DeclaringType.FullName + ".";

行之前
Type calledType = Type.GetType(typeName, true);

并替换行

Type calledType = Type.GetType(typeName, true);

Type calledType = Type.GetType(str+typeName, true);

有人能帮我理解我做错了什么吗?任何建议和/或建议将不胜感激。谢谢你。

对于代码加载类,请尝试像下面的示例一样指定名称空间:

movingAvg = GetValueOfAverage("ConsoleApplication1.MovingAverage", slowAvgMethod, slowArguments);

如果type位于当前尚未加载的程序集中,则可能仍然不够。在这种情况下,您可能需要使用程序集限定名指定类型,如下所示:

movingAvg = GetValueOfAverage("ConsoleApplication1.MovingAverage, ConsoleApplication1", slowAvgMethod, slowArguments);

(第一个ConsoleApplication1。第二个ConsoleApplication1是程序集名称)

最后,在通过反射调用方法的代码中有一个错误,其中参数作为嵌套对象[]数组传递。参数参数已经作为object[]传递,所以它应该直接传递给InvokeMember()方法:

double avg  = (double)calledType.InvokeMember(
               methodName,
               BindingFlags.InvokeMethod | BindingFlags.Public | 
               BindingFlags.Static | BindingFlags.NonPublic,
               null,
               null,
               arguments);

我更新了这篇文章,用完整的例子来可视化如何传递类型。下面的代码编译为ConsoleApplication1.exe

using System;
using System.Reflection;
namespace MyNamespace
{
    public static class MovingAverage
    {
        public static double MyMethod(int Period, int CurrentBar, int[] Input, int[] average)
        {
            return 5;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var arguments = new object[] { 1, 2, new[] { 2 }, new[] { 4 } };
            Console.WriteLine(GetValueOfAverage("MyNamespace.MovingAverage", "MyMethod", arguments)); //if assembly is already loaded
            Console.WriteLine(GetValueOfAverage("MyNamespace.MovingAverage, ConsoleApplication1", "MyMethod", arguments)); //if assembly is not loaded yet
            Console.WriteLine(GetValueOfAverage(typeof(MovingAverage), "MyMethod", arguments)); //known type
            Console.ReadKey();
        }
        public static double GetValueOfAverage(string typeName, string methodName, object[] arguments)
        {
            // ... get the Type for the class
            Type calledType = Type.GetType(typeName, true);
            return GetValueOfAverage(calledType, methodName, arguments);
        }
        public static double GetValueOfAverage(Type calledType, string methodName, object[] arguments)
        {
            double avg = (double)calledType.InvokeMember(
                           methodName,
                           BindingFlags.InvokeMethod | BindingFlags.Public |
                           BindingFlags.Static | BindingFlags.NonPublic,
                           null,
                           null,
                           arguments);
            // Return the value returned by the called method.
            return avg;
        }
    }
}

您需要完全限定MovingAverage类型

Type.GetType("MovingAverage");

不能工作,除非你覆盖了typerresolve事件。

你需要使用

Type.GetType("MovingAverageNamespace.MovingAverage, MovingAverageDllWithoutExtension");

相关内容

  • 没有找到相关文章

最新更新