C#:使用Reflection动态调用显式实现的方法

时间:2014-07-18 15:49:47

标签: c# reflection invoke

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

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;
    }
}

我收到以下错误:

无法加载类型' MovingAverage'来自汇编&f; e9d550bd4d44ddbdc78abaffa07d31,Version = 7.0.1000.22,Culture = neutral,PublicKeyToken = null'。

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

在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);

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

2 个答案:

答案 0 :(得分:1)

对于代码加载类,请尝试指定名称空间,如下例所示:

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

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

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

(其中第一个ConsoleApplication1。是名称空间,第二个ConsoleApplication1是程序集名称)

最后,你通过反射在代码调用方法中有一个错误,其中参数作为嵌套的object []数组传递。 Arguments 参数已作为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;
        }
    }
}

答案 1 :(得分:0)

您需要完全限定MovingAverage类型

Type.GetType("MovingAverage");
除非您覆盖了TypeResolve事件,否则

将无效。

您需要使用

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