1

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. */);
Nikos Baxevanis
  • 10,868
  • 2
  • 46
  • 80
  • "write the following code via Reflection" - do you mean with Reflection.Emit or what? What a lambda typically compiles to first is an expression, you you might want to actually create an expression and then have that generate the required IL code. – Lucero Nov 20 '11 at 22:21
  • Hi, not with Reflection.Emit. It could be possible to go with Emit but I would like to do it with the types defined in the System.Reflection namespace. – Nikos Baxevanis Nov 20 '11 at 22:23
  • Well, Reflection (without emit) does not generate any code, however as I said expressions are your closest friend here. – Lucero Nov 20 '11 at 22:34

3 Answers3

5

Yes, it is possible to do what you suggest via reflection, however quite unnecessary. It would be simpler to define a static method yourself like this:

public static class MyClass
{
    public static T CreateFakeWithArgumentsForConstructor<T>(object[] argumentsForConstructor)
    {
        return A.Fake<T>(x => x.WithArgumentsForConstructor(argumentsForConstructor));
    }
}

Now simply call this function using reflection:

var method = typeof(MyClass).GetMethod("CreateFakeWithArgumentsForConstructor", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(new[] { theType });
method.Invoke(null, argumentsForConstructor);
Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60
1

If I've understood the question correctly, the following should be fine:

Action<IFakeOptionsBuilder<Foo>> fakeOptionsBuilderAction = 
    o => o.WithArgumentsForConstructor(new[] { "", "" });

// You need the BindingFlags as Fake() is a static method:
object fake = typeof(A)
    .GetMethod("Fake", BindingFlags.Public | BindingFlags.Static)
    .MakeGenericMethod(new[] { typeof(Foo) })
    .Invoke(null, new object[] { fakeOptionsBuilderAction });

With Foo defied as:

class Foo
{
    public Foo(string one, string two)
    {
    }
}
Steve Wilkes
  • 7,085
  • 3
  • 29
  • 32
1

Finally! :)

The difficult part was that, on runtime, I don't know the types so generics is not an option (not even on the private helper method).

The scenario is to be able to do this:

var fake = new Fake<Foo>(o => o.WithArgumentsForConstructor("Hello"));

Here is the solution I got:

private IEnumerable<object> argumentsForConstructor;

public object Invoke(IEnumerable<object> parameters)
{
    this.argumentsForConstructor = parameters;

    Type actionType = typeof(Action<>).MakeGenericType(
         typeof(IFakeOptionsBuilder<>).MakeGenericType(this.targetType));

    MethodInfo actionMethod = this.GetType()
        .GetMethod("SetArgumentsForConstructor", BindingFlags.Instance | BindingFlags.NonPublic)
        .MakeGenericMethod(new[] { this.targetType });

    Delegate action = Delegate.CreateDelegate(actionType, this, actionMethod);

    Type fake = typeof(Fake<>).MakeGenericType(this.targetType);
    ConstructorInfo ctor = (from ci in fake.GetConstructors(BindingFlags.Instance | BindingFlags.Public)
                            from pi in ci.GetParameters()
                            where pi.ParameterType == actionType
                            select ci).First();

    return ctor.Invoke(new[] { action });
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This method is used by Reflection. It describes the method that is passed in the Action<IFakeOptionsBuilder<T>> overload of the Fake<T> constructor.")]
private void SetArgumentsForConstructor<T>(IFakeOptionsBuilder<T> o)
{
    if (typeof(T).IsInterface)
    {
        return;
    }

    o.WithArgumentsForConstructor(this.argumentsForConstructor);
}

Works like a Charm. :)

Nikos Baxevanis
  • 10,868
  • 2
  • 46
  • 80