当前位置: 首页>>代码示例>>C#>>正文


C# Expression.Clone方法代码示例

本文整理汇总了C#中Expression.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# Expression.Clone方法的具体用法?C# Expression.Clone怎么用?C# Expression.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Expression的用法示例。


在下文中一共展示了Expression.Clone方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InvertConditionInternal

		static Expression InvertConditionInternal(Expression condition)
		{
			if (condition is ParenthesizedExpression) {
				return new ParenthesizedExpression(InvertCondition(((ParenthesizedExpression)condition).Expression));
			}
			
			if (condition is UnaryOperatorExpression) {
				var uOp = (UnaryOperatorExpression)condition;
				if (uOp.Operator == UnaryOperatorType.Not) {
					if (!(uOp.Parent is Expression))
						return GetInnerMostExpression(uOp.Expression).Clone();
					return uOp.Expression.Clone();
				}
				return new UnaryOperatorExpression(UnaryOperatorType.Not, uOp.Clone());
			}
			
			if (condition is BinaryOperatorExpression) {
				var bOp = (BinaryOperatorExpression)condition;

				if ((bOp.Operator == BinaryOperatorType.ConditionalAnd) || (bOp.Operator == BinaryOperatorType.ConditionalOr)) {
					return new BinaryOperatorExpression(InvertCondition(bOp.Left), NegateConditionOperator(bOp.Operator), InvertCondition(bOp.Right));
				} else if ((bOp.Operator == BinaryOperatorType.Equality) || (bOp.Operator == BinaryOperatorType.InEquality) || (bOp.Operator == BinaryOperatorType.GreaterThan)
					|| (bOp.Operator == BinaryOperatorType.GreaterThanOrEqual) || (bOp.Operator == BinaryOperatorType.LessThan) || 
					(bOp.Operator == BinaryOperatorType.LessThanOrEqual)) {
					return new BinaryOperatorExpression(bOp.Left.Clone(), NegateRelationalOperator(bOp.Operator), bOp.Right.Clone());
				} else {
					var negatedOp = NegateRelationalOperator(bOp.Operator);
					if (negatedOp == BinaryOperatorType.Any)
						return new UnaryOperatorExpression(UnaryOperatorType.Not, new ParenthesizedExpression(condition.Clone()));
					bOp = (BinaryOperatorExpression)bOp.Clone();
					bOp.Operator = negatedOp;
					return bOp;
				}
			}
			if (condition is ConditionalExpression) {
				var cEx = condition.Clone() as ConditionalExpression;
				cEx.Condition = InvertCondition(cEx.Condition);
				return cEx;
			}
			if (condition is PrimitiveExpression) {
				var pex = condition as PrimitiveExpression;
				if (pex.Value is bool) {
					return new PrimitiveExpression(!((bool)pex.Value)); 
				}
			}
			
			return new UnaryOperatorExpression(UnaryOperatorType.Not, AddParensForUnaryExpressionIfRequired(condition.Clone()));
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:48,代码来源:CSharpUtil.cs

示例2: CommutativeOperator

		/// <summary>
		/// Produces a choice pattern for <c>expr1 op expr2</c> or <c>expr2 op expr1</c>.
		/// </summary>
		public static Expression CommutativeOperator(Expression expr1, BinaryOperatorType op, Expression expr2)
		{
			return new Choice {
				new BinaryOperatorExpression(expr1, op, expr2),
				new BinaryOperatorExpression(expr2.Clone(), op, expr1.Clone())
			};
		}
开发者ID:adisik,项目名称:simple-assembly-explorer,代码行数:10,代码来源:PatternHelper.cs

示例3: CreateFromExpression

		CodeAction CreateFromExpression(RefactoringContext context, Expression expression)
		{
			var resolveResult = context.Resolve(expression);
			if (resolveResult.IsError)
				return null;
			
			return new CodeAction(context.TranslateString("Extract method"), script => {
				string methodName = "NewMethod";
				var method = new MethodDeclaration() {
					ReturnType = context.CreateShortType(resolveResult.Type),
					Name = methodName,
					Body = new BlockStatement() {
						new ReturnStatement(expression.Clone())
					}
				};
				if (!StaticVisitor.UsesNotStaticMember(context, expression))
					method.Modifiers |= Modifiers.Static;
				var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method);

				Action<Task> replaceStatements = delegate {
					var target = new IdentifierExpression(methodName);
					script.Replace(expression, new InvocationExpression(target));
					script.Link(target, method.NameToken);
				};

				if (task.IsCompleted) {
					replaceStatements (null);
				} else {
					task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ());
				}
			});
		}
开发者ID:adisik,项目名称:simple-assembly-explorer,代码行数:32,代码来源:ExtractMethodAction.cs

示例4: InsertRequired

		string InsertRequired(Expression expr)
		{
			expr = expr.Clone();
			expr.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = false }, null);
			StringWriter w = new StringWriter();
			expr.AcceptVisitor(new OutputVisitor(w, policy), null);
			return w.ToString();
		}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:8,代码来源:InsertParenthesesVisitorTests.cs

示例5: InsertRequired

		string InsertRequired(Expression expr)
		{
			expr = expr.Clone();
			expr.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = false }, null);
			StringWriter w = new StringWriter();
			w.NewLine = " ";
			expr.AcceptVisitor(new OutputVisitor(new TextWriterOutputFormatter(w) { IndentationString = "" }, policy), null);
			return w.ToString();
		}
开发者ID:qjlee,项目名称:ILSpy,代码行数:9,代码来源:InsertParenthesesVisitorTests.cs

示例6: InsertReadable

		string InsertReadable(Expression expr)
		{
			expr = expr.Clone();
			expr.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });
			StringWriter w = new StringWriter();
			w.NewLine = " ";
			expr.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w) { IndentationString = "" }, policy));
			return w.ToString();
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:9,代码来源:InsertParenthesesVisitorTests.cs

示例7: CreateFromExpression

        CodeAction CreateFromExpression(RefactoringContext context, Expression expression)
        {
            var resolveResult = context.Resolve(expression);
            if (resolveResult.IsError)
                return null;

            return new CodeAction(context.TranslateString("Extract method"), script => {
                string methodName = "NewMethod";
                var method = new MethodDeclaration {
                    ReturnType = context.CreateShortType(resolveResult.Type),
                    Name = methodName,
                    Body = new BlockStatement {
                        new ReturnStatement(expression.Clone())
                    }
                };
                if (!StaticVisitor.UsesNotStaticMember(context, expression))
                    method.Modifiers |= Modifiers.Static;

                var usedVariables = VariableLookupVisitor.Analyze(context, expression);

                var inExtractedRegion = new VariableUsageAnalyzation (context, usedVariables);

                usedVariables.Sort ((l, r) => l.Region.Begin.CompareTo (r.Region.Begin));
                var target = new IdentifierExpression(methodName);
                var invocation = new InvocationExpression(target);
                foreach (var variable in usedVariables) {
                    Expression argumentExpression = new IdentifierExpression(variable.Name);

                    var mod = ParameterModifier.None;
                    if (inExtractedRegion.GetStatus (variable) == VariableState.Changed) {
                        mod = ParameterModifier.Ref;
                        argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression);
                    }

                    method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(variable.Type), variable.Name, mod));
                    invocation.Arguments.Add(argumentExpression);
                }

                var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method);

                Action<Task> replaceStatements = delegate {
                    script.Replace(expression, invocation);
                    script.Link(target, method.NameToken);
                };

                if (task.IsCompleted) {
                    replaceStatements (null);
                } else {
                    task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ());
                }
            }, expression);
        }
开发者ID:segaman,项目名称:NRefactory,代码行数:52,代码来源:ExtractMethodAction.cs

示例8: CreateDebugListExpression

        /// <summary>
        /// Creates an expression which, when evaluated, creates a List&lt;T&gt; in the debugee
        /// filled with contents of IEnumerable&lt;T&gt; from the debugee.
        /// </summary>
        /// <param name="iEnumerableVariable">Expression for IEnumerable variable in the debugee.</param>
        /// <param name="itemType">
        /// The generic argument of IEnumerable&lt;T&gt; that <paramref name="iEnumerableVariable"/> implements.</param>
        public static Expression CreateDebugListExpression(Expression iEnumerableVariable, DebugType itemType, out DebugType listType)
        {
            // is using itemType.AppDomain ok?
            listType = DebugType.CreateFromType(itemType.AppDomain, typeof(System.Collections.Generic.List<>), itemType);
            var iEnumerableType = DebugType.CreateFromType(itemType.AppDomain, typeof(IEnumerable<>), itemType);
            // explicitely cast the variable to IEnumerable<T>, where T is itemType
            Expression iEnumerableVariableExplicitCast = new CastExpression { Expression = iEnumerableVariable.Clone() , Type = iEnumerableType.GetTypeReference() };

            var obj = new ObjectCreateExpression() { Type = listType.GetTypeReference() };
            obj.Arguments.Add(iEnumerableVariableExplicitCast);

            return obj;
        }
开发者ID:BahNahNah,项目名称:dnSpy,代码行数:20,代码来源:DebuggerHelper.cs

示例9: OptimizeExpression

        public static Expression OptimizeExpression(Expression node)
        {
            var root = new ParenthesizedExpression(node.Clone());
            var visitors = GetVisitors();
            string oldText = null;
            while (root.GetText() != oldText)
            {
                oldText = root.GetText();
                foreach (var visitor in visitors)
                    root.Expression.AcceptVisitor(visitor);
            }

            return root.Expression;
        }
开发者ID:TreeSeed,项目名称:Tychaia,代码行数:14,代码来源:AstHelpers.cs

示例10: CreateFromExpression

		CodeAction CreateFromExpression(RefactoringContext context, Expression expression)
		{
			var resolveResult = context.Resolve(expression);
			if (resolveResult.IsError)
				return null;

			return new CodeAction(context.TranslateString("Extract method"), script => {
				string methodName = "NewMethod";
				var method = new MethodDeclaration() {
					ReturnType = context.CreateShortType(resolveResult.Type),
					Name = methodName,
					Body = new BlockStatement() {
						new ReturnStatement(expression.Clone())
					}
				};
				if (!StaticVisitor.UsesNotStaticMember(context, expression))
					method.Modifiers |= Modifiers.Static;
				script.InsertWithCursor(context.TranslateString("Extract method"), method, Script.InsertPosition.Before);
				var target = new IdentifierExpression(methodName);
				script.Replace(expression, new InvocationExpression(target));
//				script.Link(target, method.NameToken);
			});
		}
开发者ID:KAW0,项目名称:Alter-Native,代码行数:23,代码来源:ExtractMethodAction.cs

示例11: AddIsNaNIssue

			void AddIsNaNIssue(BinaryOperatorExpression binaryOperatorExpr, Expression argExpr, string floatType)
			{
				if (ctx.Resolve (argExpr).Type.ReflectionName != "System.Single")
					floatType = "double";
				AddIssue (binaryOperatorExpr, string.Format(ctx.TranslateString ("Use {0}.IsNan()"), floatType),
					script => {
						Expression expr = new InvocationExpression (new MemberReferenceExpression (
							new TypeReferenceExpression (new PrimitiveType (floatType)), "IsNaN"), argExpr.Clone ());
						if (binaryOperatorExpr.Operator == BinaryOperatorType.InEquality)
							expr = new UnaryOperatorExpression (UnaryOperatorType.Not, expr);
						script.Replace (binaryOperatorExpr, expr);
					});
			}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:13,代码来源:CompareFloatWithEqualityOperatorIssue.cs

示例12: InvertCondition

		public static Expression InvertCondition (Expression condition)
		{
			return InvertConditionInternal (condition.Clone ());
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:4,代码来源:CSharpUtil.cs

示例13: MakeForeach

		static ForeachStatement MakeForeach(Expression node, IType type, RefactoringContext context)
		{
			var namingHelper = new NamingHelper(context);
			return new ForeachStatement() {
				VariableType = new SimpleType("var"),
				VariableName = namingHelper.GenerateVariableName(type),
				InExpression = node.Clone(),
				EmbeddedStatement = new BlockStatement()
			};
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:10,代码来源:IterateViaForeachAction.cs

示例14: DetermineRelativeOffsetForLayerFromXYZ

 /// <summary>
 /// Applies adjustments to an expression when calculating the relative offset for a layer.
 /// </summary>
 private static PrimitiveExpression DetermineRelativeOffsetForLayerFromXYZ(
     Expression startIJK,
     Expression xyz,
     Expression minXYZ,
     string xyzName)
 {
     return EvaluateExpression(
         new UnaryOperatorExpression(
             UnaryOperatorType.Minus,
             new BinaryOperatorExpression(
                 startIJK.Clone(),
                 BinaryOperatorType.Subtract,
                 new BinaryOperatorExpression(
                     xyz.Clone(),
                     BinaryOperatorType.Subtract,
                     minXYZ.Clone()))),
         new Dictionary<string, object> { { xyzName, 0 } });
 }
开发者ID:TreeSeed,项目名称:Tychaia,代码行数:21,代码来源:RangedLayer.cs

示例15: RefactorAllIdentifiers

 public static void RefactorAllIdentifiers(AstNode node, string identifier, Expression newIdentifier)
 {
     for (int i = 0; i < node.Children.Count(); i++)
     {
         AstNode n = node.Children.ElementAt(i);
         if (n is IdentifierExpression)
         {
             IdentifierExpression id = n as IdentifierExpression;
             if (id.Identifier.Equals(identifier))
             {
                 n.ReplaceWith(newIdentifier.Clone());
             }
         }
         else
         {
             RefactorAllIdentifiers(n, identifier, newIdentifier);
         }
     }
 }
开发者ID:AlexAlbala,项目名称:Alter-Native,代码行数:19,代码来源:Resolver.cs


注:本文中的Expression.Clone方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。