1

リフレクション経由で次のコードを書くことは可能ですか?

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

o次のとおりです。

Action<IFakeOptionsBuilder<T>>

WithArgumentsForConstructorの場所:

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

Foo クラスは次のとおりです。

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

私がしたことは:

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

3 に答える 3

5

はい、リフレクションを介して提案することは可能ですが、まったく不要です。次のように静的メソッドを自分で定義する方が簡単です。

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

リフレクションを使用してこの関数を呼び出すだけです。

var method = typeof(MyClass).GetMethod("CreateFakeWithArgumentsForConstructor", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(new[] { theType });
method.Invoke(null, argumentsForConstructor);
于 2011-11-22T16:37:28.500 に答える
1

質問を正しく理解していれば、次の問題は問題ありません。

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 });

次のようにFoo反抗します:

class Foo
{
    public Foo(string one, string two)
    {
    }
}
于 2011-11-20T23:23:42.830 に答える
1

ついに!:)

難しい部分は、実行時に型がわからないため、ジェネリックはオプションではありません (プライベートヘルパーメソッドでも)。

シナリオは、これを実行できるようにすることです。

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

これが私が得た解決策です:

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);
}

チャームのように機能します。:)

于 2011-11-23T09:47:13.460 に答える