Using Reflection Invoke static generic method passing a Lamba as parameter

Posted by Nikos Baxevanis on Stack Overflow See other posts from Stack Overflow or by Nikos Baxevanis
Published on 2011-11-20T22:18:31Z Indexed on 2011/11/23 9:50 UTC
Read the original article Hit count: 301

Filed under:
|
|
|

Is it possible to write the following code via Reflection?

    var fake = A.Fake<Foo>(
            o => o.WithArgumentsForConstructor(new[] { "Hello" }));

Where o is:

Action<IFakeOptionsBuilder<T>>

Where WithArgumentsForConstructor is:

IFakeOptionsBuilder<T> WithArgumentsForConstructor(IEnumerable<object> argumentsForConstructor);

The Foo class is:

class Foo
{
    public Foo(string s)
    {
    }
}

What I did was:

object fake = typeof(A)
    .GetMethod("Fake", new Type[] { })
    .MakeGenericMethod(new[] { this.targetType })
    .Invoke(null, /* Here I need to pass the lambda. */);

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection