0

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]'.

1 Answers1

1

Your funcMembers contain this:

funcMembers.Add("Foundation", () => {return 4;}); 

the right-hand-side is an anonymous delegate, parameterless, returning constant 4. It is implicitely typed as Func<object> soit can be added to the dictionary. As all delegates, compiler compiles it as a class, but since it doesn't have name, a special name of "d__14`1[System.Object]" is automatically generated.

Then, it seems that either Generator or GetEnumerable method return this delegate object directly, instead of invoking it and getting the value of 4 and wrapping it into IEnumerable.

This delegate returned from Generator or GetEnumerable is then assigned to s.Foundation, which causes this error you noticed (since the anonymous class of the delegate obviously doesn't implement IEnumerable).

I bet you can see all of that simply by stepping with the debugger.For a better view, write it like that:

   var tmp = Generator(
                     "Foundation",
                      typeof(School).GetField("Foundation")...
   s.Foundation = tmp;

and observe the value in TMP and then, well, diagnose what's up (ie. by stepping into Generator and seeing what happens there) and fix it.

Side note: This is as far as got with these code scraps you pasted. I can't tell you more about your problem, because the code you provided contains severe errors (i.e Generator - not all code paths return value), some lines are incomplete (where's the end of typeof(School).GetField("Foundation").?) and so on.

In such cases, please try to provide minimal complete example which actually compiles.

Community
  • 1
  • 1
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107