mercredi 5 août 2015

Cast result of MethodInfo.Invoke to actual type

This question is related to a problem of mine mention on this topic. However the problem here is somewhat new because I want to cast the result of MethodInfo.Invoke to its actual type.

So I have a couple of methods with this signature: IEnumerable<MyClass> myFunc (ITable table). As all those methods expect an instance of ITable I wanted to write a single test-method that executes them all and checks if they all throw an exception if argument is not provided. This is done via reflection by selecting the methods within my testclass that expect that interface as an argument:

var funcs = typeof(MyCollector).GetMethods().Where(x => x.GetParameters().Any(y => typeof(ITable).IsAssignableFrom(y.ParameterType))).ToList();

Now I loop those methods and call them:

foreach (var func in funcs)
{   
    IEnumerable<MyClass> features = (IEnumerable<MyClass>) func.Invoke(myCollector, args); }
}

As all those methods are executed delayed (using yield return) what they actually return is an enumerator instead of the enumeration itself - at least this is what I guess is going on. This casting the result to IEnumerable<MyClass> will fail. Furthermore I cannpt debug INTo that method which is probably because it does execute delayed.

What am I missing to get this to work?





Aucun commentaire:

Enregistrer un commentaire