我正在尝试使用反射来动态调用显式实现的方法。我的方法如下:
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 []数组传递。参数参数已作为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;
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句