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


C# PropertyDeclarationSyntax.WithInitializer方法代码示例

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


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

示例1: VisitPropertyDeclaration

		/// <summary>
		///   Normalizes the <paramref name="declaration" />.
		/// </summary>
		public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax declaration)
		{
			var objectCreation = declaration?.Initializer?.Value as ObjectCreationExpressionSyntax;
			if (objectCreation == null)
				return declaration;

			var symbol = declaration.GetPropertySymbol(SemanticModel);

			var fault = SemanticModel.GetTypeSymbol<Fault>();
			if (!symbol.Type.Equals(fault) && !symbol.Type.IsDerivedFrom(fault))
				return declaration;

			return declaration.WithInitializer(declaration.Initializer.WithValue(AddNameInitializer(fault, objectCreation, symbol.Name)));
		}
开发者ID:isse-augsburg,项目名称:ssharp,代码行数:17,代码来源:FaultNameNormalizer.cs

示例2: VisitPropertyDeclaration

		/// <summary>
		///   Normalizes the <paramref name="declaration" />.
		/// </summary>
		public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax declaration)
		{
			// Nothing to do here for properties with expression bodies
			if (declaration.ExpressionBody != null)
				return declaration;

			// Nothing to do here for properties not defined in fault effects or for properties that are no overrides of some port
			var propertySymbol = declaration.GetPropertySymbol(SemanticModel);
			if (!propertySymbol.ContainingType.IsFaultEffect(SemanticModel) || !propertySymbol.IsOverride)
				return declaration;

			// Nothing to do here for required ports
			if (propertySymbol.IsExtern)
				return declaration;

			var getter = (AccessorDeclarationSyntax)Syntax.GetAccessor(declaration, DeclarationKind.GetAccessor);
			var setter = (AccessorDeclarationSyntax)Syntax.GetAccessor(declaration, DeclarationKind.SetAccessor);

			// Nothing to do here for properties with accessors that have a body
			if (getter == null || getter.Body != null || setter?.Body != null)
				return declaration;

			var fieldName = GetBackingFieldName(propertySymbol);
			var fieldIdentifier = (ExpressionSyntax)Syntax.IdentifierName(fieldName);
			var fieldModifiers = setter == null
				? DeclarationModifiers.Unsafe | DeclarationModifiers.ReadOnly
				: DeclarationModifiers.Unsafe;
			var fieldDeclaration = Syntax.FieldDeclaration(
				name: fieldName,
				type: Syntax.TypeExpression(propertySymbol.Type),
				accessibility: Accessibility.Private,
				modifiers: fieldModifiers,
				initializer: declaration.Initializer?.Value);

			fieldDeclaration = Syntax.AddAttribute<CompilerGeneratedAttribute>(fieldDeclaration);
			fieldDeclaration = Syntax.MarkAsNonDebuggerBrowsable(fieldDeclaration);
			AddMembers(propertySymbol.ContainingType, (FieldDeclarationSyntax)fieldDeclaration);

			var getterBody = (StatementSyntax)Syntax.ReturnStatement(fieldIdentifier).NormalizeWhitespace();
			getterBody = getterBody.AppendLineDirective(-1).PrependLineDirective(-1);
			var getterBlock = SyntaxFactory.Block(getterBody).EnsureIndentation(getter).PrependLineDirective(getter.GetLineNumber());
			getter = getter.WithStatementBody(getterBlock);

			AccessorListSyntax accessors;
			if (setter == null)
				accessors = declaration.AccessorList.WithAccessors(SyntaxFactory.List(new[] { getter }));
			else
			{
				var assignment = Syntax.AssignmentStatement(fieldIdentifier, Syntax.IdentifierName("value"));
				var setterBody = (StatementSyntax)Syntax.ExpressionStatement(assignment);
				setterBody = setterBody.AppendLineDirective(-1).PrependLineDirective(-1);
				var setterBlock = SyntaxFactory.Block(setterBody).EnsureIndentation(setter).PrependLineDirective(setter.GetLineNumber());
				setter = setter.WithStatementBody(setterBlock);

				accessors = declaration.AccessorList.WithAccessors(SyntaxFactory.List(new[] { getter, setter }));
			}

			var originalDeclaration = declaration;
			declaration = declaration.WithInitializer(null).WithSemicolonToken(default(SyntaxToken));
			return declaration.WithAccessorList(accessors).EnsureLineCount(originalDeclaration);
		}
开发者ID:isse-augsburg,项目名称:ssharp,代码行数:64,代码来源:AutoPropertyNormalizer.cs


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