2

ExpressionVisitor(オプションで)NullReferenceException最初の値にスローする式を生成するカスタムを作成しようとしていnullます。式のDebugViewは私には問題ないように見えますが、期待どおりに機能しません(私によると)。最初に投げると思った

.Throw .New System.NullReferenceException("c3")

テスト変数はありますがnull、代わりにこれがスローされるためです

.Throw .New System.NullReferenceException("p")

ステートメントを逆方向に実行する理由がわかりません。If最初に最も内側を実行するべきではありませんか?

デバッグビュー:

.Block() {
    .If (.Block() {
        .If (.Constant<ExpressionTrees.Program+<>c__DisplayClass0_0>(ExpressionTrees.Program+<>c__DisplayClass0_0) == null) {
            .Throw .New System.NullReferenceException("c3")
        } .Else {
            .Default(System.Void)
        };
        .Constant<ExpressionTrees.Program+<>c__DisplayClass0_0>(ExpressionTrees.Program+<>c__DisplayClass0_0).c3
    } == null) {
        .Throw .New System.NullReferenceException("p")
    } .Else {
        .Default(System.Void)
    };
    (.Constant<ExpressionTrees.Program+<>c__DisplayClass0_0>(ExpressionTrees.Program+<>c__DisplayClass0_0).c3).p
}

私の完全なテストコード:

namespace ExpressionTrees
{
    class c1
    {
        public c2 c2 { get; set; }
    }

    class c2
    {
        public c3 c3 { get; set; }
    }

    class c3
    {
        public string p { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            c3 c3 = null; 
            var test_c3 = NullGuard.Check(() => c3.p, true);
        }
    }

    public static class NullGuard
    {
        public static T Check<T>(Expression<Func<T>> expression, bool canThrowNullReferenceException = false)
        {
            var nullGuardVisitor = new NullGuardVisitor(canThrowNullReferenceException);
            var nullGuardExpression = nullGuardVisitor.Visit(expression.Body);            
            var nullGuardLambda = Expression.Lambda<Func<T>>(nullGuardExpression, expression.Parameters);
            var value = nullGuardLambda.Compile()();
            return value;
        }
    }

    public class NullGuardVisitor : ExpressionVisitor
    {
        private readonly bool _canThrowNullReferenceException;

        internal NullGuardVisitor(bool canThrowNullReferenceException)
        {
            _canThrowNullReferenceException = canThrowNullReferenceException;
        }

        protected override Expression VisitMember(MemberExpression node)
        {
            var expression = Visit(node.Expression);

            // expression == null
            var expressionEqualsNull = Expression.Equal(expression, Expression.Constant(null, expression.Type));

            if (_canThrowNullReferenceException)
            {
                var nullReferenceExceptionConstructorInfo = typeof(NullReferenceException).GetConstructor(new[] { typeof(string) });

                // if (expression == null) { throw new NullReferenceException() } else { node }
                var result = 
                    Expression.Block(
                        Expression.IfThen(
                            expressionEqualsNull,
                            Expression.Throw(Expression.New(nullReferenceExceptionConstructorInfo, Expression.Constant(node.Member.Name)))
                        ),
                        node
                    );
                return result;
            }
            else
            {
                var result = Expression.Condition(
                    expressionEqualsNull,
                    Expression.Constant(null, expression.Type),
                    node);
                return result;
            }

        }
    }
}
4

2 に答える 2

4

想定どおりに機能しています。

これは、空白と行番号が異なる同じデバッグ ビューです。

1   .Block() 
2   {
3       .If (.Block() 
4       {
5           .If (.Constant<ExpressionTrees.Program+<>c__DisplayClass0_0>(ExpressionTrees.Program+<>c__DisplayClass0_0) == null) 
6           {
7               .Throw .New System.NullReferenceException("c3")
8           } 
9           .Else 
10          {
11              .Default(System.Void)
12          };
13          .Constant<ExpressionTrees.Program+<>c__DisplayClass0_0>(ExpressionTrees.Program+<>c__DisplayClass0_0).c3
14      } == null) 
15      {
16          .Throw .New System.NullReferenceException("p")
17      } .Else 
18      {
19          .Default(System.Void)
20      };
21      (.Constant<ExpressionTrees.Program+<>c__DisplayClass0_0>(ExpressionTrees.Program+<>c__DisplayClass0_0).c3).p
22  }

13 行目を見てください if ((closure class).c3 == null) throw new NullReferenceException("p")。最初のチェック (5 行目) は事実上if ((closure class) == null) throw new NullReferenceException("c3"). 問題は何よりも誤解を招く例外メッセージにあります。

于 2016-01-13T22:30:09.080 に答える
3

問題は次のスニペットにあります。

Expression.Throw(Expression.New(nullReferenceExceptionConstructorInfo,
    Expression.Constant(node.Member.Name)))

このメンバー アクセスがa.b. チェックしてから、チェックしたばかりなのにnullaというメッセージで例外をスローしています。ba

コンテナーが null だったためにアクセスできなかったというよりも、node.Expressionそれが null であると言いたい場合は、例外メッセージで使用する必要があります。ab

于 2016-01-13T22:34:16.030 に答える