コード例 #1
0
        private static ExpressionToken Visit(LambdaExpression exp)
        {
            CompositeExpressionToken token = new CompositeExpressionToken();
            string s = "";

            if (exp.Parameters.Count != 1)
            {
                s = "(";
            }
            s = s + string.Join(", ", (from p in exp.Parameters select CleanIdentifier(p.Name)).ToArray <string>());
            if (exp.Parameters.Count != 1)
            {
                s = s + ")";
            }
            s = s + " => ";
            token.AddStringToken(s);
            ExpressionToken item = Visit(exp.Body);

            if (item != null)
            {
                item.Splittable  = true;
                item.SplitIndent = 1;
                token.Tokens.Add(item);
            }
            return(token);
        }
コード例 #2
0
        private static ExpressionToken Visit(NewArrayExpression exp)
        {
            bool flag = exp.NodeType == ExpressionType.NewArrayInit;
            CompositeExpressionToken token = new CompositeExpressionToken();
            bool flag2 = exp.Type.IsAnonymous();
            Type t     = exp.Type;

            if (t.IsArray)
            {
                t = t.GetElementType();
            }
            string str = flag2 ? "" : t.FormatTypeName();
            string s   = "new " + str;

            if (flag && exp.Expressions.Any <Expression>())
            {
                s = s + "[] ";
            }
            else if (flag)
            {
                s = s + "[0]";
            }
            token.AddStringToken(s);
            if (!flag || exp.Expressions.Any <Expression>())
            {
                token.Tokens.Add(new BracketedExpressionToken(flag ? "{ " : "[", flag ? " } " : "]", new CompositeExpressionToken(from a in exp.Expressions select Visit(a), true)));
            }
            return(token);
        }
コード例 #3
0
        private static ExpressionToken Visit(InvocationExpression exp)
        {
            CompositeExpressionToken token = new CompositeExpressionToken();

            token.AddStringToken("Invoke" + ((exp.Arguments.Count == 0) ? "" : " "), true);
            token.Tokens.Add(new BracketedExpressionToken("(", ")", new CompositeExpressionToken(from a in exp.Arguments select Visit(a), true)));
            return(token);
        }
コード例 #4
0
        private static ExpressionToken Visit(MemberAssignment mb)
        {
            CompositeExpressionToken token = new CompositeExpressionToken();

            token.AddStringToken(CleanIdentifier(mb.Member.Name) + " = ");
            token.Tokens.Add(Visit(mb.Expression));
            token.Splittable = true;
            return(token);
        }
コード例 #5
0
        private static ExpressionToken Visit(TypeBinaryExpression exp)
        {
            CompositeExpressionToken body = new CompositeExpressionToken {
                Tokens = { Visit(exp.Expression) }
            };

            body.AddStringToken(" is ");
            body.AddStringToken(exp.TypeOperand.FormatTypeName());
            return(new BracketedExpressionToken("(", ")", body));
        }
コード例 #6
0
        private static ExpressionToken Visit(MethodCallExpression exp)
        {
            Func <PropertyInfo, bool> predicate = null;
            bool   flag = Attribute.IsDefined(exp.Method, typeof(ExtensionAttribute));
            string name = exp.Method.Name;
            CompositeExpressionToken token = new CompositeExpressionToken();

            if (flag)
            {
                if (exp.Method.DeclaringType == typeof(Queryable))
                {
                    token.MultiLine = true;
                }
                token.Tokens.Add(Visit(exp.Arguments[0]));
                token.AddStringToken("." + name + " ", true);
                token.Tokens.Last <ExpressionToken>().SplitIndent = 1;
                CompositeExpressionToken token2 = new CompositeExpressionToken(from a in exp.Arguments.Skip <Expression>(1) select Visit(a), true);
                if ((exp.Method.DeclaringType == typeof(Queryable)) && (exp.Arguments.Count <Expression>() > 2))
                {
                    token2.MultiLine = true;
                }
                token2.SplitIndent = 1;
                token.Tokens.Add(new BracketedExpressionToken("(", ")", token2));
                return(token);
            }
            if (exp.Object == null)
            {
                token.AddStringToken(exp.Method.DeclaringType.FormatTypeName());
            }
            else
            {
                token.Tokens.Add(Visit(exp.Object));
            }
            if (exp.Method.IsSpecialName)
            {
                if (predicate == null)
                {
                    predicate = p => p.GetAccessors().Contains <MethodInfo>(exp.Method);
                }
                if (exp.Method.DeclaringType.GetProperties().Where <PropertyInfo>(predicate).FirstOrDefault <PropertyInfo>() != null)
                {
                    token.Tokens.Add(new BracketedExpressionToken(" [", "]", new CompositeExpressionToken(from a in exp.Arguments select Visit(a), true)));
                    return(token);
                }
            }
            token.AddStringToken("." + name + ((exp.Arguments.Count == 0) ? "" : " "), true);
            token.Tokens.Last <ExpressionToken>().SplitIndent = 1;
            CompositeExpressionToken body = new CompositeExpressionToken(from a in exp.Arguments select Visit(a), true)
            {
                SplitIndent = 1
            };

            token.Tokens.Add(new BracketedExpressionToken("(", ")", body));
            return(token);
        }
コード例 #7
0
        private static ExpressionToken Visit(ConditionalExpression exp)
        {
            CompositeExpressionToken token = new CompositeExpressionToken {
                Tokens = { Visit(exp.Test) }
            };

            token.AddStringToken(" ? ", true);
            token.Tokens.Add(Visit(exp.IfTrue));
            token.AddStringToken(" : ", true);
            token.Tokens.Add(Visit(exp.IfFalse));
            return(token);
        }
コード例 #8
0
        private static ExpressionToken Visit(ListInitExpression exp)
        {
            CompositeExpressionToken token = new CompositeExpressionToken {
                Tokens = { Visit(exp.NewExpression) }
            };
            CompositeExpressionToken body = new CompositeExpressionToken {
                AddCommas = true
            };

            foreach (ElementInit init in exp.Initializers)
            {
                body.Tokens.Add(new BracketedExpressionToken("(", ")", true, new CompositeExpressionToken(from a in init.Arguments select Visit(a), true)));
            }
            token.Tokens.Add(new BracketedExpressionToken(" { ", " } ", body));
            return(token);
        }
コード例 #9
0
        private static ExpressionToken Visit(NewExpression exp)
        {
            CompositeExpressionToken token = new CompositeExpressionToken();
            Type declaringType             = exp.Type;

            if (exp.Constructor != null)
            {
                declaringType = exp.Constructor.DeclaringType;
            }
            token.AddStringToken("new " + (declaringType.IsAnonymous() ? "" : declaringType.FormatTypeName()) + ((exp.Arguments.Count == 0) ? "" : " "));
            if ((exp.Members == null) || (exp.Members.Count == 0))
            {
                CompositeExpressionToken token2 = new CompositeExpressionToken(from a in exp.Arguments select Visit(a), true);
                token.Tokens.Add(new BracketedExpressionToken("(", ")", token2));
                return(token);
            }
            int num = 0;
            CompositeExpressionToken body = new CompositeExpressionToken {
                MultiLine = true,
                AddCommas = true
            };

            foreach (Expression expression in exp.Arguments)
            {
                MemberInfo   mi   = exp.Members[num++];
                PropertyInfo info = mi as PropertyInfo;
                if (info == null)
                {
                    info = mi.DeclaringType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).FirstOrDefault <PropertyInfo>(p => p.GetAccessors().Contains <MemberInfo>(mi));
                }
                if (info != null)
                {
                    CompositeExpressionToken token5 = new CompositeExpressionToken();
                    token5.AddStringToken(CleanIdentifier(info.Name) + " = ");
                    token5.Tokens.Add(Visit(expression));
                    token5.Splittable = true;
                    body.Tokens.Add(token5);
                }
            }
            BracketedExpressionToken item = new BracketedExpressionToken("{", "}", body)
            {
                NewLineBefore = true
            };

            token.Tokens.Add(item);
            return(token);
        }
コード例 #10
0
        private static ExpressionToken Visit(UnaryExpression exp)
        {
            if (exp.NodeType == ExpressionType.Quote)
            {
                return(Visit(exp.Operand));
            }
            CompositeExpressionToken token2 = new CompositeExpressionToken();

            switch (exp.NodeType)
            {
            case ExpressionType.Negate:
            case ExpressionType.NegateChecked:
                token2.AddStringToken("-");
                break;

            case ExpressionType.UnaryPlus:
                token2.AddStringToken("+");
                break;

            case ExpressionType.Not:
                token2.AddStringToken("!");
                break;

            case ExpressionType.Convert:
                if (exp.Operand.Type.IsSubclassOf(exp.Type))
                {
                    return(Visit(exp.Operand));
                }
                token2.AddStringToken("(" + exp.Type.FormatTypeName() + ")");
                break;

            case ExpressionType.TypeAs:
                token2.Tokens.Add(Visit(exp.Operand));
                token2.AddStringToken(" as ");
                token2.AddStringToken(exp.Type.FormatTypeName());
                return(token2);

            default:
                token2.AddStringToken(exp.NodeType.ToString());
                break;
            }
            token2.Tokens.Add(new BracketedExpressionToken("(", ")", true, Visit(exp.Operand)));
            return(token2);
        }
コード例 #11
0
        private static ExpressionToken Visit(MemberExpression exp)
        {
            CompositeExpressionToken token      = new CompositeExpressionToken();
            ConstantExpression       expression = exp.Expression as ConstantExpression;

            if (!((((expression == null) || (expression.Value == null)) || (!expression.Value.GetType().IsNested || !expression.Value.GetType().Name.StartsWith("<", StringComparison.Ordinal))) ? ((expression == null) || !(expression.Value is DataContext)) : false))
            {
                return(new LeafExpressionToken(exp.Member.Name));
            }
            if (exp.Expression != null)
            {
                token.Tokens.Add(Visit(exp.Expression));
            }
            else
            {
                token.AddStringToken(exp.Member.DeclaringType.Name);
            }
            token.AddStringToken("." + CleanIdentifier(exp.Member.Name));
            return(token);
        }
コード例 #12
0
        private static ExpressionToken Visit(MemberInitExpression exp)
        {
            CompositeExpressionToken token = new CompositeExpressionToken();

            if (exp.NewExpression.Type.IsAnonymous())
            {
                token.AddStringToken("new ");
            }
            else
            {
                token.Tokens.Add(Visit(exp.NewExpression));
            }
            CompositeExpressionToken body = new CompositeExpressionToken(from b in exp.Bindings select Visit(b), true)
            {
                MultiLine = true
            };
            BracketedExpressionToken item = new BracketedExpressionToken("{", "}", body)
            {
                NewLineBefore = true
            };

            token.Tokens.Add(item);
            return(token);
        }
コード例 #13
0
        private static ExpressionToken Visit(BinaryExpression exp)
        {
            string str;
            CompositeExpressionToken body   = new CompositeExpressionToken();
            ExpressionToken          item   = Visit(exp.Left);
            ExpressionToken          token3 = Visit(exp.Right);

            if (exp.NodeType == ExpressionType.ArrayIndex)
            {
                body.Tokens.Add(item);
                body.Tokens.Add(new BracketedExpressionToken("[", "]", token3));
                return(body);
            }
            if (_binaryExpressionStrings.TryGetValue(exp.NodeType, out str))
            {
                body.Tokens.Add(item);
                body.AddStringToken(" " + str + " ");
                body.Tokens.Add(token3);
                token3.Splittable  = true;
                token3.SplitIndent = 1;
                return(new BracketedExpressionToken("(", ")", true, body));
            }
            return(body);
        }
コード例 #14
0
 private static ExpressionToken Visit(NewExpression exp)
 {
     CompositeExpressionToken token = new CompositeExpressionToken();
     Type declaringType = exp.Type;
     if (exp.Constructor != null)
     {
         declaringType = exp.Constructor.DeclaringType;
     }
     token.AddStringToken("new " + (declaringType.IsAnonymous() ? "" : declaringType.FormatTypeName()) + ((exp.Arguments.Count == 0) ? "" : " "));
     if ((exp.Members == null) || (exp.Members.Count == 0))
     {
         CompositeExpressionToken token2 = new CompositeExpressionToken(from a in exp.Arguments select Visit(a), true);
         token.Tokens.Add(new BracketedExpressionToken("(", ")", token2));
         return token;
     }
     int num = 0;
     CompositeExpressionToken body = new CompositeExpressionToken {
         MultiLine = true,
         AddCommas = true
     };
     foreach (Expression expression in exp.Arguments)
     {
         MemberInfo mi = exp.Members[num++];
         PropertyInfo info = mi as PropertyInfo;
         if (info == null)
         {
             info = mi.DeclaringType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).FirstOrDefault<PropertyInfo>(p => p.GetAccessors().Contains<MemberInfo>(mi));
         }
         if (info != null)
         {
             CompositeExpressionToken token5 = new CompositeExpressionToken();
             token5.AddStringToken(CleanIdentifier(info.Name) + " = ");
             token5.Tokens.Add(Visit(expression));
             token5.Splittable = true;
             body.Tokens.Add(token5);
         }
     }
     BracketedExpressionToken item = new BracketedExpressionToken("{", "}", body) {
         NewLineBefore = true
     };
     token.Tokens.Add(item);
     return token;
 }
コード例 #15
0
 private static ExpressionToken Visit(TypeBinaryExpression exp)
 {
     CompositeExpressionToken body = new CompositeExpressionToken {
         Tokens = { Visit(exp.Expression) }
     };
     body.AddStringToken(" is ");
     body.AddStringToken(exp.TypeOperand.FormatTypeName());
     return new BracketedExpressionToken("(", ")", body);
 }
コード例 #16
0
 private static ExpressionToken Visit(MethodCallExpression exp)
 {
     Func<PropertyInfo, bool> predicate = null;
     bool flag = Attribute.IsDefined(exp.Method, typeof(ExtensionAttribute));
     string name = exp.Method.Name;
     CompositeExpressionToken token = new CompositeExpressionToken();
     if (flag)
     {
         if (exp.Method.DeclaringType == typeof(Queryable))
         {
             token.MultiLine = true;
         }
         token.Tokens.Add(Visit(exp.Arguments[0]));
         token.AddStringToken("." + name + " ", true);
         token.Tokens.Last<ExpressionToken>().SplitIndent = 1;
         CompositeExpressionToken token2 = new CompositeExpressionToken(from a in exp.Arguments.Skip<Expression>(1) select Visit(a), true);
         if ((exp.Method.DeclaringType == typeof(Queryable)) && (exp.Arguments.Count<Expression>() > 2))
         {
             token2.MultiLine = true;
         }
         token2.SplitIndent = 1;
         token.Tokens.Add(new BracketedExpressionToken("(", ")", token2));
         return token;
     }
     if (exp.Object == null)
     {
         token.AddStringToken(exp.Method.DeclaringType.FormatTypeName());
     }
     else
     {
         token.Tokens.Add(Visit(exp.Object));
     }
     if (exp.Method.IsSpecialName)
     {
         if (predicate == null)
         {
             predicate = p => p.GetAccessors().Contains<MethodInfo>(exp.Method);
         }
         if (exp.Method.DeclaringType.GetProperties().Where<PropertyInfo>(predicate).FirstOrDefault<PropertyInfo>() != null)
         {
             token.Tokens.Add(new BracketedExpressionToken(" [", "]", new CompositeExpressionToken(from a in exp.Arguments select Visit(a), true)));
             return token;
         }
     }
     token.AddStringToken("." + name + ((exp.Arguments.Count == 0) ? "" : " "), true);
     token.Tokens.Last<ExpressionToken>().SplitIndent = 1;
     CompositeExpressionToken body = new CompositeExpressionToken(from a in exp.Arguments select Visit(a), true) {
         SplitIndent = 1
     };
     token.Tokens.Add(new BracketedExpressionToken("(", ")", body));
     return token;
 }
コード例 #17
0
 private static ExpressionToken Visit(NewArrayExpression exp)
 {
     bool flag = exp.NodeType == ExpressionType.NewArrayInit;
     CompositeExpressionToken token = new CompositeExpressionToken();
     bool flag2 = exp.Type.IsAnonymous();
     Type t = exp.Type;
     if (t.IsArray)
     {
         t = t.GetElementType();
     }
     string str = flag2 ? "" : t.FormatTypeName();
     string s = "new " + str;
     if (flag && exp.Expressions.Any<Expression>())
     {
         s = s + "[] ";
     }
     else if (flag)
     {
         s = s + "[0]";
     }
     token.AddStringToken(s);
     if (!flag || exp.Expressions.Any<Expression>())
     {
         token.Tokens.Add(new BracketedExpressionToken(flag ? "{ " : "[", flag ? " } " : "]", new CompositeExpressionToken(from a in exp.Expressions select Visit(a), true)));
     }
     return token;
 }
コード例 #18
0
 private static ExpressionToken Visit(MemberExpression exp)
 {
     CompositeExpressionToken token = new CompositeExpressionToken();
     ConstantExpression expression = exp.Expression as ConstantExpression;
     if (!((((expression == null) || (expression.Value == null)) || (!expression.Value.GetType().IsNested || !expression.Value.GetType().Name.StartsWith("<", StringComparison.Ordinal))) ? ((expression == null) || !(expression.Value is DataContext)) : false))
     {
         return new LeafExpressionToken(exp.Member.Name);
     }
     if (exp.Expression != null)
     {
         token.Tokens.Add(Visit(exp.Expression));
     }
     else
     {
         token.AddStringToken(exp.Member.DeclaringType.Name);
     }
     token.AddStringToken("." + CleanIdentifier(exp.Member.Name));
     return token;
 }
コード例 #19
0
 private static ExpressionToken Visit(MemberInitExpression exp)
 {
     CompositeExpressionToken token = new CompositeExpressionToken();
     if (exp.NewExpression.Type.IsAnonymous())
     {
         token.AddStringToken("new ");
     }
     else
     {
         token.Tokens.Add(Visit(exp.NewExpression));
     }
     CompositeExpressionToken body = new CompositeExpressionToken(from b in exp.Bindings select Visit(b), true) {
         MultiLine = true
     };
     BracketedExpressionToken item = new BracketedExpressionToken("{", "}", body) {
         NewLineBefore = true
     };
     token.Tokens.Add(item);
     return token;
 }
コード例 #20
0
 private static ExpressionToken Visit(MemberAssignment mb)
 {
     CompositeExpressionToken token = new CompositeExpressionToken();
     token.AddStringToken(CleanIdentifier(mb.Member.Name) + " = ");
     token.Tokens.Add(Visit(mb.Expression));
     token.Splittable = true;
     return token;
 }
コード例 #21
0
 private static ExpressionToken Visit(ListInitExpression exp)
 {
     CompositeExpressionToken token = new CompositeExpressionToken {
         Tokens = { Visit(exp.NewExpression) }
     };
     CompositeExpressionToken body = new CompositeExpressionToken {
         AddCommas = true
     };
     foreach (ElementInit init in exp.Initializers)
     {
         body.Tokens.Add(new BracketedExpressionToken("(", ")", true, new CompositeExpressionToken(from a in init.Arguments select Visit(a), true)));
     }
     token.Tokens.Add(new BracketedExpressionToken(" { ", " } ", body));
     return token;
 }
コード例 #22
0
 private static ExpressionToken Visit(LambdaExpression exp)
 {
     CompositeExpressionToken token = new CompositeExpressionToken();
     string s = "";
     if (exp.Parameters.Count != 1)
     {
         s = "(";
     }
     s = s + string.Join(", ", (from p in exp.Parameters select CleanIdentifier(p.Name)).ToArray<string>());
     if (exp.Parameters.Count != 1)
     {
         s = s + ")";
     }
     s = s + " => ";
     token.AddStringToken(s);
     ExpressionToken item = Visit(exp.Body);
     if (item != null)
     {
         item.Splittable = true;
         item.SplitIndent = 1;
         token.Tokens.Add(item);
     }
     return token;
 }
コード例 #23
0
 private static ExpressionToken Visit(InvocationExpression exp)
 {
     CompositeExpressionToken token = new CompositeExpressionToken();
     token.AddStringToken("Invoke" + ((exp.Arguments.Count == 0) ? "" : " "), true);
     token.Tokens.Add(new BracketedExpressionToken("(", ")", new CompositeExpressionToken(from a in exp.Arguments select Visit(a), true)));
     return token;
 }
コード例 #24
0
        private static ExpressionToken Visit(UnaryExpression exp)
        {
            if (exp.NodeType == ExpressionType.Quote)
            {
                return Visit(exp.Operand);
            }
            CompositeExpressionToken token2 = new CompositeExpressionToken();
            switch (exp.NodeType)
            {
                case ExpressionType.Negate:
                case ExpressionType.NegateChecked:
                    token2.AddStringToken("-");
                    break;

                case ExpressionType.UnaryPlus:
                    token2.AddStringToken("+");
                    break;

                case ExpressionType.Not:
                    token2.AddStringToken("!");
                    break;

                case ExpressionType.Convert:
                    if (exp.Operand.Type.IsSubclassOf(exp.Type))
                    {
                        return Visit(exp.Operand);
                    }
                    token2.AddStringToken("(" + exp.Type.FormatTypeName() + ")");
                    break;

                case ExpressionType.TypeAs:
                    token2.Tokens.Add(Visit(exp.Operand));
                    token2.AddStringToken(" as ");
                    token2.AddStringToken(exp.Type.FormatTypeName());
                    return token2;

                default:
                    token2.AddStringToken(exp.NodeType.ToString());
                    break;
            }
            token2.Tokens.Add(new BracketedExpressionToken("(", ")", true, Visit(exp.Operand)));
            return token2;
        }
コード例 #25
0
 private static ExpressionToken Visit(BinaryExpression exp)
 {
     string str;
     CompositeExpressionToken body = new CompositeExpressionToken();
     ExpressionToken item = Visit(exp.Left);
     ExpressionToken token3 = Visit(exp.Right);
     if (exp.NodeType == ExpressionType.ArrayIndex)
     {
         body.Tokens.Add(item);
         body.Tokens.Add(new BracketedExpressionToken("[", "]", token3));
         return body;
     }
     if (_binaryExpressionStrings.TryGetValue(exp.NodeType, out str))
     {
         body.Tokens.Add(item);
         body.AddStringToken(" " + str + " ");
         body.Tokens.Add(token3);
         token3.Splittable = true;
         token3.SplitIndent = 1;
         return new BracketedExpressionToken("(", ")", true, body);
     }
     return body;
 }
コード例 #26
0
 private static ExpressionToken Visit(ConditionalExpression exp)
 {
     CompositeExpressionToken token = new CompositeExpressionToken {
         Tokens = { Visit(exp.Test) }
     };
     token.AddStringToken(" ? ", true);
     token.Tokens.Add(Visit(exp.IfTrue));
     token.AddStringToken(" : ", true);
     token.Tokens.Add(Visit(exp.IfFalse));
     return token;
 }