Using Reflection Invoke static generic method passing a Lamba as parameter
- by Nikos Baxevanis
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. */);