コード例 #1
0
ファイル: TranslateEngine.cs プロジェクト: zyj0021/Mego
        private DbExpression RootTranslate(TranslationContext context, Expression exp)
        {
            switch (exp.NodeType)
            {
            case ExpressionType.Call:
                var call   = (MethodCallExpression)exp;
                var method = call.Method;
                if (method.IsGenericMethod)
                {
                    method = method.GetGenericMethodDefinition();
                }
                if (UnitTypeMethods.ContainsKey(method))
                {
                    return(UnitTypeTranslate(context, exp));
                }
                else if (UnitItemTypeMethods.ContainsKey(method))
                {
                    return(UnitItemTypeMethods[method](context, call, method));
                }
                else if (ValueTypeMethods.ContainsKey(method))
                {
                    return(ValueTypeMethods[method](context, call, method));
                }
                break;

            case ExpressionType.Constant:
                return(UnitTypeTranslate(context, exp));

            default:
                return(ExpressionTranslate(context, exp));
            }
            throw new TranslateException(exp, Res.NotSupportedParseExpression);
        }
コード例 #2
0
        private DbUnitTypeExpression UnitTypeTranslate(TranslationContext context, Expression exp)
        {
            switch (exp.NodeType)
            {
            case ExpressionType.Call:
                var callExpression = (MethodCallExpression)exp;
                var method         = callExpression.Method;
                if (method.IsGenericMethod)
                {
                    method = method.GetGenericMethodDefinition();
                }
                if (UnitTypeMethods.TryGetValue(method, out UnitTypeTranslateMethodType action))
                {
                    return(action(context, callExpression));
                }
                break;

            case ExpressionType.Constant:
                var constant = (ConstantExpression)exp;
                if (constant.Value is IDbSet dbset)
                {
                    if (context.Context != dbset.Context)
                    {
                        throw new NotSupportedException(Res.NotSupportedContextIsNotMatch);
                    }
                    return(new DbDataSetExpression(constant.Type, dbset.Name));
                }
                throw new TranslateException(exp, Res.ExceptionIsMustDbSetInstance);

            case ExpressionType.MemberAccess:
                var member = (MemberExpression)exp;
                if (member.Expression.NodeType == ExpressionType.MemberAccess)
                {
                    var subexp = (MemberExpression)member.Expression;
                    if (subexp.Expression.NodeType == ExpressionType.Constant)
                    {
                        var obj = Expression.Lambda(member).Compile().DynamicInvoke();
                        if (obj is IDbSet dbset2)
                        {
                            if (context.Context != dbset2.Context)
                            {
                                throw new NotSupportedException(Res.NotSupportedContextIsNotMatch);
                            }
                            return(new DbDataSetExpression(exp.Type, dbset2.Name));
                        }
                        throw new TranslateException(exp, Res.ExceptionIsMustDbSetInstance);
                    }
                    if (ExpressionTranslate(context, member) is DbUnitTypeExpression unitExpression)
                    {
                        return(unitExpression);
                    }
                }
                else
                {
                    if (ExpressionTranslate(context, member) is DbUnitTypeExpression unitExpression)
                    {
                        return(unitExpression);
                    }
                }
                break;
            }
            throw new TranslateException(exp, Res.NotSupportedParseUnitType);
        }