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


C# CSharpOutputVisitor.VisitParameterDeclaration方法代码示例

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


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

示例1: GenerateCode

		protected override string GenerateCode(ITypeDefinition currentClass)
		{
			List<PropertyOrFieldWrapper> filtered = parameterList
				.Where(p => p.IsIncluded)
				.OrderBy(p => p.Index)
				.ToList();

			var test = refactoringContext.GetNode();
			var insertedConstructor = refactoringContext.GetNode().PrevSibling as ConstructorDeclaration;
			if (insertedConstructor == null)
			{
				// We are not inside of a constructor declaration
				return null;
			}
			
			using (Script script = refactoringContext.StartScript()) {
				BlockStatement originalCtorBody = insertedConstructor.Body;
				
				foreach (PropertyOrFieldWrapper w in filtered) {
					if (w.AddCheckForNull) {
						// true = reference, null = generic or unknown
						if (w.Type.IsReferenceType != false)
							script.AddTo(originalCtorBody,
							             new IfElseStatement(
							             	new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.Equality, new PrimitiveExpression(null)),
							             	new ThrowStatement(new ObjectCreateExpression(new SimpleType("ArgumentNullException"), new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"') }))
							             )
							            );
						else
							script.AddTo(originalCtorBody,
							             new IfElseStatement(
							             	new UnaryOperatorExpression(UnaryOperatorType.Not, new MemberReferenceExpression(new IdentifierExpression(w.MemberName), "HasValue")),
							             	new ThrowStatement(new ObjectCreateExpression(new SimpleType("ArgumentNullException"), new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"') }))
							             )
							            );
					}
					if (w.AddRangeCheck) {
						script.AddTo(originalCtorBody,
						             new IfElseStatement(
						             	new BinaryOperatorExpression(
						             		new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.LessThan, new IdentifierExpression("lower")),
						             		BinaryOperatorType.ConditionalOr,
						             		new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.GreaterThan, new IdentifierExpression("upper"))
						             	),
						             	new ThrowStatement(
						             		new ObjectCreateExpression(
						             			new SimpleType("ArgumentOutOfRangeException"),
						             			new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"'), new IdentifierExpression(w.ParameterName), new BinaryOperatorExpression(new PrimitiveExpression("Value must be between "), BinaryOperatorType.Add, new BinaryOperatorExpression(new IdentifierExpression("lower"), BinaryOperatorType.Add, new BinaryOperatorExpression(new PrimitiveExpression(" and "), BinaryOperatorType.Add, new IdentifierExpression("upper")))) }
						             		)
						             	)
						             )
						            );
					}
				}
				
				foreach (PropertyOrFieldWrapper w in filtered) {
					script.AddTo(originalCtorBody,
					             new ExpressionStatement(new AssignmentExpression(new MemberReferenceExpression(new ThisReferenceExpression(), w.MemberName), AssignmentOperatorType.Assign, new IdentifierExpression(w.ParameterName)))
					            );
				}
			}
			
			AnchorElement parameterListElement = insertionContext.ActiveElements
				.OfType<AnchorElement>()
				.FirstOrDefault(item => item.Name.Equals("parameterList", StringComparison.OrdinalIgnoreCase));

			if (parameterListElement != null) {
				StringBuilder pList = new StringBuilder();

				var parameters = filtered
					.Select(p => new ParameterDeclaration(refactoringContext.CreateShortType(p.Type), p.ParameterName))
					.ToList();

				using (StringWriter textWriter = new StringWriter(pList)) {
					// Output parameter list as string
					var formattingOptions = FormattingOptionsFactory.CreateMono();
					CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor(textWriter, formattingOptions);
					for (int i = 0; i < parameters.Count; i++) {
						if (i > 0)
							textWriter.Write(",");
						outputVisitor.VisitParameterDeclaration(parameters[i]);
					}
				}

				parameterListElement.Text = pList.ToString();
			}
			
			return null;
		}
开发者ID:Paccc,项目名称:SharpDevelop,代码行数:89,代码来源:InsertCtorDialog.xaml.cs


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