本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.PropertyDeclarationSyntax类的典型用法代码示例。如果您正苦于以下问题:C# PropertyDeclarationSyntax类的具体用法?C# PropertyDeclarationSyntax怎么用?C# PropertyDeclarationSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PropertyDeclarationSyntax类属于Microsoft.CodeAnalysis.CSharp.Syntax命名空间,在下文中一共展示了PropertyDeclarationSyntax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitPropertyDeclaration
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
return base.VisitPropertyDeclaration(
node.WithType(UpdateType(node.Type))
.WithModifiers(SyntaxFactory.TokenList())
.WithAccessorList(UpdateAccessorList(node.AccessorList, node.Type)));
}
示例2: VisitPropertyDeclaration
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
if (RequiresNullChecks(node.AttributeLists))
{
AddMessage(node, $"property {node.Identifier} needs null checks");
}
}
示例3: IsPropertyCandidate
private static bool IsPropertyCandidate(PropertyDeclarationSyntax propertySyntax, SemanticModel semanticModel)
{
if (HasDocumentationComment(propertySyntax))
{
return false;
}
var propertySymbol = semanticModel.GetDeclaredSymbol(propertySyntax);
if (propertySymbol == null ||
!propertySymbol.IsOverride ||
propertySymbol.IsSealed ||
propertySymbol.OverriddenProperty == null)
{
return false;
}
if (propertySymbol.GetMethod != null && propertySymbol.OverriddenProperty.GetMethod == null)
{
return false;
}
if (propertySymbol.SetMethod != null && propertySymbol.OverriddenProperty.SetMethod == null)
{
return false;
}
return CheckGetAccessorIfAny(propertySyntax, propertySymbol, semanticModel) &&
CheckSetAccessorIfAny(propertySyntax, propertySymbol, semanticModel);
}
示例4: VisitPropertyDeclaration
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
_cancellationToken.ThrowIfCancellationRequested();
var propertySymbol = _semanticModel.GetDeclaredSymbol(node, _cancellationToken);
var propertyAssumptions = _propertyAssumptions.Where(pa => pa.Property == propertySymbol).ToList();
if (propertyAssumptions.Count == 0)
{
return base.VisitPropertyDeclaration(node);
}
var newProperty = node;
foreach (var propertyAssumption in propertyAssumptions)
{
foreach (string attributeToDelete in propertyAssumption.GetAttributesToDelete(_objectTheoremResult))
{
newProperty = RemoveAttribute(newProperty, attributeToDelete);
}
foreach (string attributeToAdd in propertyAssumption.GetAttributesToAdd(_objectTheoremResult))
{
newProperty = EnsureAttribute(newProperty, attributeToAdd);
}
foreach (AttributeSyntax attributeToAdd in propertyAssumption.GetAttributeSyntaxexToAdd(_objectTheoremResult))
{
newProperty = EnsureAttribute(newProperty, attributeToAdd);
}
}
return base.VisitPropertyDeclaration(newProperty);
}
示例5: HandleProperty
private Diagnostic HandleProperty(PropertyDeclarationSyntax propertyDeclaration)
{
if (propertyDeclaration.ExpressionBody != null)
{
return null;
}
if (propertyDeclaration.DescendantNodesAndTokensAndSelf().Any(x => x.GetLeadingTrivia().Concat(x.GetTrailingTrivia()).Any(y => !y.IsWhitespaceTrivia())))
{
return null;
}
var getter = propertyDeclaration.AccessorList.Accessors.FirstOrDefault(x => x.Keyword.ValueText == "get");
if (getter == null)
{
return null;
}
if (getter.AttributeLists.Any(x => x.Attributes.Any()))
{
return null;
}
if (getter.Body?.Statements.Count != 1)
{
return null;
}
var statement = getter.Body.Statements.First();
return Diagnostic.Create(Rule, statement.GetLocation(), "Property", propertyDeclaration.Identifier);
}
示例6: MakeAutoPropertyAsync
public async static Task<Solution> MakeAutoPropertyAsync(Document document, SyntaxNode root, PropertyDeclarationSyntax property, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
var getterReturn = (ReturnStatementSyntax)property.AccessorList.Accessors.First(a => a.Keyword.ValueText == "get").Body.Statements.First();
var returnIdentifier = (IdentifierNameSyntax)(getterReturn.Expression is MemberAccessExpressionSyntax ? ((MemberAccessExpressionSyntax)getterReturn.Expression).Name : getterReturn.Expression);
var returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol;
var variableDeclarator = (VariableDeclaratorSyntax)returnIdentifierSymbol.DeclaringSyntaxReferences.First().GetSyntax();
var fieldDeclaration = variableDeclarator.FirstAncestorOfType<FieldDeclarationSyntax>();
root = root.TrackNodes(returnIdentifier, fieldDeclaration, property);
document = document.WithSyntaxRoot(root);
root = await document.GetSyntaxRootAsync(cancellationToken);
semanticModel = await document.GetSemanticModelAsync(cancellationToken);
returnIdentifier = root.GetCurrentNode(returnIdentifier);
returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol;
var newProperty = GetSimpleProperty(property, variableDeclarator)
.WithTriviaFrom(property)
.WithAdditionalAnnotations(Formatter.Annotation);
var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, returnIdentifierSymbol, property.Identifier.ValueText, document.Project.Solution.Workspace.Options, cancellationToken);
document = newSolution.GetDocument(document.Id);
root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
root = root.InsertNodesAfter(root.GetCurrentNode(property), new[] { newProperty });
var multipleVariableDeclaration = fieldDeclaration.Declaration.Variables.Count > 1;
if (multipleVariableDeclaration)
{
var newfieldDeclaration = fieldDeclaration.WithDeclaration(fieldDeclaration.Declaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepNoTrivia));
root = root.RemoveNode(root.GetCurrentNode<SyntaxNode>(property), SyntaxRemoveOptions.KeepNoTrivia);
root = root.ReplaceNode(root.GetCurrentNode(fieldDeclaration), newfieldDeclaration);
}
else
{
root = root.RemoveNodes(root.GetCurrentNodes<SyntaxNode>(new SyntaxNode[] { fieldDeclaration, property }), SyntaxRemoveOptions.KeepNoTrivia);
}
document = document.WithSyntaxRoot(root);
return document.Project.Solution;
}
示例7: Property
public Property(SemanticModel model, PropertyDeclarationSyntax syntax, AttributeSyntax attribute = null, AttributeSyntax classAttribute = null)
{
this.model = model;
Syntax = syntax;
propertyAttribute = attribute;
ClassAttribute = classAttribute;
}
示例8: MakeAutoPropertyAsync
public async static Task<Solution> MakeAutoPropertyAsync(Document document, SyntaxNode root, PropertyDeclarationSyntax property, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
var getterReturn = (ReturnStatementSyntax)property.AccessorList.Accessors.First(a => a.Keyword.ValueText == "get").Body.Statements.First();
var returnIdentifier = (IdentifierNameSyntax)(getterReturn.Expression is MemberAccessExpressionSyntax
? ((MemberAccessExpressionSyntax)getterReturn.Expression).Name
: getterReturn.Expression);
var fieldSymbol = (IFieldSymbol)semanticModel.GetSymbolInfo(returnIdentifier).Symbol;
var variableDeclarator = (VariableDeclaratorSyntax)fieldSymbol.DeclaringSyntaxReferences.First().GetSyntax();
var fieldDeclaration = variableDeclarator.FirstAncestorOfType<FieldDeclarationSyntax>();
var propertySymbol = semanticModel.GetDeclaredSymbol(property);
var newRoot = root.TrackNodes(returnIdentifier, fieldDeclaration, property, variableDeclarator);
//cycle
var newDocument = document.WithSyntaxRoot(newRoot);
newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken);
var newProperty = CreateAutoProperty(property, variableDeclarator, fieldSymbol, propertySymbol);
Solution newSolution;
if (IsExplicityImplementation(propertySymbol))
{
semanticModel = await newDocument.GetSemanticModelAsync(cancellationToken);
returnIdentifier = newRoot.GetCurrentNode(returnIdentifier);
fieldSymbol = (IFieldSymbol)semanticModel.GetSymbolInfo(returnIdentifier).Symbol;
newSolution = await RenameSymbolAndKeepExplicitPropertiesBoundAsync(newDocument.Project.Solution, property.Identifier.ValueText, fieldSymbol, propertySymbol, cancellationToken);
newDocument = newSolution.GetDocument(newDocument.Id);
newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
newRoot = newRoot.ReplaceNode(newRoot.GetCurrentNode(property), newProperty);
newSolution = newSolution.WithDocumentSyntaxRoot(newDocument.Id, newRoot);
}
else
{
var currentProperty = newRoot.GetCurrentNode(property);
var type = (TypeDeclarationSyntax)currentProperty.Parent;
var propertyIndex = type.Members.IndexOf(currentProperty);
//Remove the property: this is needed otherwise the rename that happens bellow will not be able to
//correctly redirect the references to the field, as the property will conflict with the name.
//The conflict is specially troublesome for circular references, such as the one that caused the bug #702.
newRoot = newRoot.ReplaceNode(type, type.RemoveNode(currentProperty, SyntaxRemoveOptions.KeepNoTrivia));
//cycle
newDocument = newDocument.WithSyntaxRoot(newRoot);
newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken);
semanticModel = await newDocument.GetSemanticModelAsync(cancellationToken);
fieldSymbol = (IFieldSymbol)semanticModel.GetDeclaredSymbol(newRoot.GetCurrentNode(variableDeclarator));
//rename the field:
newSolution = await Renamer.RenameSymbolAsync(newDocument.Project.Solution, fieldSymbol, property.Identifier.ValueText, newDocument.Project.Solution.Workspace.Options, cancellationToken).ConfigureAwait(false);
//cycle
newDocument = newSolution.GetDocument(newDocument.Id);
newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
//add the property back:
var currentType = (TypeDeclarationSyntax)newRoot.GetCurrentNode(fieldDeclaration).Parent;
var newMembers = currentType.Members.Insert(propertyIndex, newProperty);
var newType = WithMembers(currentType, newMembers);
newRoot = newRoot.ReplaceNode(currentType, newType);
newSolution = newSolution.WithDocumentSyntaxRoot(newDocument.Id, newRoot);
}
newDocument = newSolution.GetDocument(newDocument.Id);
newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
newRoot = RemoveField(newRoot, variableDeclarator, fieldDeclaration);
return newSolution.WithDocumentSyntaxRoot(newDocument.Id, newRoot);
}
示例9: VisitPropertyDeclaration
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax propertySyntax)
{
var leadingTrivia = propertySyntax.Identifier.LeadingTrivia;
var trailingTriva = propertySyntax.Identifier.TrailingTrivia;
return propertySyntax.ReplaceToken(propertySyntax.Identifier,
SyntaxFactory.Identifier(leadingTrivia,
ToCamelCase(propertySyntax.Identifier.ValueText), trailingTriva) );
}
示例10: IsPropertyBoolean
/// <summary>
/// Return true if the supplied property is a boolean property.
/// </summary>
/// <param name="propertyNode">the property node.</param>
/// <returns>true if the property is a boolean property.</returns>
public bool IsPropertyBoolean(PropertyDeclarationSyntax propertyNode)
{
if (propertyNode.Type.Kind() != SyntaxKind.PredefinedType)
return false;
var predefined = propertyNode.Type as PredefinedTypeSyntax;
return predefined?.Keyword.Kind() == SyntaxKind.BoolKeyword;
}
示例11: IsAutoImplementedProperty
private bool IsAutoImplementedProperty(PropertyDeclarationSyntax propertyDecl)
{
SyntaxList<AccessorDeclarationSyntax> accessors = propertyDecl.AccessorList.Accessors;
AccessorDeclarationSyntax getter = accessors.FirstOrDefault(ad => ad.Kind() == SyntaxKind.GetAccessorDeclaration);
AccessorDeclarationSyntax setter = accessors.FirstOrDefault(ad => ad.Kind() == SyntaxKind.SetAccessorDeclaration);
if (getter == null || setter == null) return false;
return getter.Body == null && setter.Body == null;
}
示例12: ChangeToFullPropertyAsync
private async Task<Document> ChangeToFullPropertyAsync(Document document, PropertyDeclarationSyntax propertyDecl, CancellationToken cancellationToken)
{
SemanticModel model = await document.GetSemanticModelAsync(cancellationToken);
var root = await document.GetSyntaxRootAsync(cancellationToken) as CompilationUnitSyntax;
document = document.WithSyntaxRoot(CodeGeneration.ImplementFullProperty(root, model, propertyDecl, document.Project.Solution.Workspace));
return document;
}
示例13: VisitPropertyDeclaration
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
// TODO: Parse properties correctly and load their nodes.
PropertyWalker walker = this.CreateSyntaxWalker<PropertyWalker>(node);
walker.Visit(node);
this.Variables.Add(new Property() { Name = node.Identifier.ToString(), Type = node.Type.ToString() });
base.VisitPropertyDeclaration(node);
}
示例14: MakeInterfaceProperty
private static PropertyDeclarationSyntax MakeInterfaceProperty(PropertyDeclarationSyntax propertySyntax)
{
var accessors = propertySyntax.AccessorList.Accessors.Select(f => MakeInterfaceAccessor(f));
var syntaxList = new SyntaxList<AccessorDeclarationSyntax>();
syntaxList = syntaxList.AddRange(accessors);
var accessorList = propertySyntax.AccessorList.WithAccessors(syntaxList);
return propertySyntax.WithModifiers(new SyntaxTokenList()).WithAccessorList(accessorList);
}
示例15: IsGetOnlyAutoProperty
public static bool IsGetOnlyAutoProperty(this IPropertySymbol property, PropertyDeclarationSyntax propertyDeclaration)
{
Contract.Requires(property != null);
Contract.Requires(propertyDeclaration != null);
var getter = propertyDeclaration.Getter();
if (getter == null) return false;
return property.IsReadOnly && getter.SemicolonToken.IsKind(SyntaxKind.SemicolonToken);
}