I'm trying to create generator of IEnumerable to set School.Foundation,
this is the Type that I'm trying to populate at the moment:
public class School
{
public IEnumerable<int> Foundation;
}
this is my generator so far:
public static IEnumerable<T> GetEnumerable <T>(Func<T> f)
{
while(true)
{
yield return f();
}
}
So what I'm doing is the following reflection:
Dictionary<string, Func<object>> funcMembers;
public static object Generator(String name, Type t)
{
Func<object> func;
if (funcMembers.TryGetValue(name, out func))
{
if (t.Name.StartsWith("IEnumerable"))
return GetEnumerable(func);
}
}
I made the following test to check if it was fully functional:
[TestMethod]
public void Test_generator()
{
private Dictionary<string, Func<object>> funcMembers = new Dictionary<string, Func<object>>();
//adding the Field "Foundation" generator on the dictionary
funcMembers.Add("Foundation", () => {return 4;});
School s = new School();
// calling my Generator that should return IEnumerable<int>
s.Foundation = Generator(
"Foundation",
typeof(School).GetField("Foundation").
Assert.IsNotNull(s.Foundation);
}
i have the following error when I'm on line:
s.Foundation = Generator(
"Foundation",
typeof(School).GetField("Foundation").
the error is the following:
> Unable to cast object of type '<GetEnumerable>d__14`1[System.Object]'
> to type 'System.Collections.Generic.IEnumerable`1[System.Int32]'.
Aucun commentaire:
Enregistrer un commentaire