C# 无法转换'&书信电报;T>;d_u14`1[System.Object]到';IEnumerable`1[System.Int32]';
我正在尝试创建IEnumerable生成器来设置School.Foundation 这就是我目前正在尝试填充的类型:C# 无法转换'&书信电报;T>;d_u14`1[System.Object]到';IEnumerable`1[System.Int32]';,c#,reflection,ienumerable,yield,system.reflection,C#,Reflection,Ienumerable,Yield,System.reflection,我正在尝试创建IEnumerable生成器来设置School.Foundation 这就是我目前正在尝试填充的类型: public class School { public IEnumerable<int> Foundation; } 错误如下: > Unable to cast object of type '<GetEnumerable>d__14`1[System.Object]' > to type 'System.Collections.
public class School
{
public IEnumerable<int> Foundation;
}
错误如下:
> Unable to cast object of type '<GetEnumerable>d__14`1[System.Object]'
> to type 'System.Collections.Generic.IEnumerable`1[System.Int32]'.
>无法强制转换类型为'd_u14'1[System.object]的对象'
>输入'System.Collections.Generic.IEnumerable'1[System.Int32]'。
您的函数成员
包含以下内容:
funcMembers.Add("Foundation", () => {return 4;});
右侧是匿名委托,无参数,返回常量4
。它隐式键入为Func
soit,可以将其添加到字典中。作为所有委托,编译器将其编译为一个类,但由于它没有名称,因此会自动生成一个特殊名称“d__14`1[System.Object]”
然后,似乎Generator
或GetEnumerable
方法直接返回此委托对象,而不是调用它并获取4
的值并将其包装为IEnumerable
从代码< >生成器> />代码>或<代码> GETCOMPIONTION/COD>返回的委托被分配给S.Fund,这导致了您注意到的错误(因为委托的匿名类显然不实现IQueDaby)。
我打赌,只需使用调试器,您就可以看到所有这些。要获得更好的视图,请这样编写: var tmp = Generator(
"Foundation",
typeof(School).GetField("Foundation")...
s.Foundation = tmp;
观察TMP中的值,然后,好的,诊断出发生了什么(即,进入发电机,看看那里发生了什么),并修复它
旁注:这是您粘贴的这些代码片段所能达到的程度。我不能告诉您更多关于您的问题,因为您提供的代码包含严重错误(即Generator
-并非所有代码路径都返回值),有些行不完整(typeof(School.GetField(“Foundation”)。?)等等
在这种情况下,请尝试提供实际编译的代码
s.Foundation = Generator(
"Foundation",
typeof(School).GetField("Foundation").
> Unable to cast object of type '<GetEnumerable>d__14`1[System.Object]'
> to type 'System.Collections.Generic.IEnumerable`1[System.Int32]'.
funcMembers.Add("Foundation", () => {return 4;});
var tmp = Generator(
"Foundation",
typeof(School).GetField("Foundation")...
s.Foundation = tmp;