反射方法调用 - 无法转换参数

时间:2015-09-12 21:42:41

标签: c# .net reflection

我有一个具有签名的静态方法:

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

1 个答案:

答案 0 :(得分:1)

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>>()