反射方法调用 - 无法转换参数
Reflection Method Invoke - Failed to convert parameters
我有一个具有签名的静态方法:
pubic static foo (float,float,IEnumberable<IEnumberable<double>>)
我正在使用反射以
的参数调用此方法
int, int, and List<List<double>>,
这失败了,在我看来,List<List<double>>
参数转换失败。我正在使用下面的代码尝试转换参数
这可能吗?反射的局限性?我本以为 List 实现了 IEnumerable 接口并且可以正常工作。
var args = inputportvals.Select(x=>
{
if (x.First is IronPython.Runtime.List || x.First is IDynamicMetaObjectProvider)
{
return x.First;
}
if (x.First is IEnumerable || x.First is IList)
{
return x.First as IEnumerable;
}
else
{
return Convert.ChangeType(x.First, infos.Where(y=>y.Name == x.Second).First().ParameterType);
}
}
).ToArray();
funcdef.MethodPointer.Invoke(null,args);
List<List<double>>
不能转换为 IEnumerable<IEnumberable<double>>
:
List<List<double>> x = null;
IEnumerable<IEnumerable<double>> y = x; //does not compile and fails with an explicit cast
您需要自己进行转换。例如:
x.Cast<IEnumerable<double>>()
我有一个具有签名的静态方法:
pubic static foo (float,float,IEnumberable<IEnumberable<double>>)
我正在使用反射以
的参数调用此方法int, int, and List<List<double>>,
这失败了,在我看来,List<List<double>>
参数转换失败。我正在使用下面的代码尝试转换参数
这可能吗?反射的局限性?我本以为 List 实现了 IEnumerable 接口并且可以正常工作。
var args = inputportvals.Select(x=>
{
if (x.First is IronPython.Runtime.List || x.First is IDynamicMetaObjectProvider)
{
return x.First;
}
if (x.First is IEnumerable || x.First is IList)
{
return x.First as IEnumerable;
}
else
{
return Convert.ChangeType(x.First, infos.Where(y=>y.Name == x.Second).First().ParameterType);
}
}
).ToArray();
funcdef.MethodPointer.Invoke(null,args);
List<List<double>>
不能转换为 IEnumerable<IEnumberable<double>>
:
List<List<double>> x = null;
IEnumerable<IEnumerable<double>> y = x; //does not compile and fails with an explicit cast
您需要自己进行转换。例如:
x.Cast<IEnumerable<double>>()