1

このテストに合格したい:

[Test]
public void LambdaTest()
{
    var m = Expression.Lambda(typeof(Func<int>), Expression.Constant(0)).Compile();
    Assert.That(m.Method.DeclaringType, Is.Not.Null);
}

これは、スタック ウォーキングの遅延コードを正しく機能させるために必要です。それを行う最も簡単な方法は何ですか?

私は最もポータブルな方法を好みます。

4

3 に答える 3

1

わかりました、私はそれを自分で見つけましたが、.NET Core でどのように動作するか、どのフレームワークがこれをサポートしているかどうかはわかりません。より優れた (よりエレガントまたはポータブルな) ソリューションがある場合は、お気軽に回答を投稿してください。

表現の使い方CompileToMethodがポイントです。Lambda

[Test]
public void LambdaTest2()
{
    var asm = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("test"), AssemblyBuilderAccess.Run);
    var masm = asm.DefineDynamicModule("main");

    var type = masm.DefineType("TestType");
    var mb = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]);

    // your lambda
    ConstantExpression expressionTree = Expression.Constant(0);
    Expression.Lambda(typeof(Func<int>), expressionTree).CompileToMethod(mb);

    var m = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), type.CreateType().GetMethod("TestMethod"));

    Assert.That(m.Method.DeclaringType, Is.Not.Null);

    // you can create another in the same module but with another type (because type can't be changed)
    var type2 = masm.DefineType("TestType2");
    var mb2 = type2.DefineMethod("TestMethod2", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]);

    // your lambda 2
    ConstantExpression expresisonTree2 = Expression.Constant(1);
    Expression.Lambda(typeof(Func<int>), expresisonTree2).CompileToMethod(mb2);

    var m2 = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), type2.CreateType().GetMethod("TestMethod2"));

    Assert.That(m2.Method.DeclaringType, Is.Not.Null);

    // check correctness
    Assert.That(m(), Is.EqualTo(0));
    Assert.That(m2(), Is.EqualTo(1));
}
于 2016-07-12T22:30:34.440 に答える