本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.PropertyDeclarationSyntax.WithAccessorList方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyDeclarationSyntax.WithAccessorList方法的具体用法?C# PropertyDeclarationSyntax.WithAccessorList怎么用?C# PropertyDeclarationSyntax.WithAccessorList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.Syntax.PropertyDeclarationSyntax
的用法示例。
在下文中一共展示了PropertyDeclarationSyntax.WithAccessorList方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSimpleProperty
private static PropertyDeclarationSyntax GetSimpleProperty(PropertyDeclarationSyntax property, VariableDeclaratorSyntax variableDeclarator)
{
var simpleGetSetPropetie = property.WithAccessorList(SyntaxFactory.AccessorList(SyntaxFactory.List(new[] {
SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
})));
return variableDeclarator.Initializer == null ?
simpleGetSetPropetie :
simpleGetSetPropetie.WithInitializer(variableDeclarator.Initializer)
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
}
示例2: ConvertToAutoProperty
private PropertyDeclarationSyntax ConvertToAutoProperty(PropertyDeclarationSyntax propertyDeclaration)
{
var newProperty = propertyDeclaration
.WithAccessorList(
SyntaxFactory.AccessorList(
SyntaxFactory.List(new[]
{
SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)).WithAdditionalAnnotations(Formatter.Annotation),
SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
})));
return newProperty;
}
示例3: ExpandProperty
private static PropertyDeclarationSyntax ExpandProperty(PropertyDeclarationSyntax original, PropertyDeclarationSyntax updated, string backingFieldName)
{
AccessorDeclarationSyntax getter = original.AccessorList.Accessors.FirstOrDefault(ad => ad.Kind() == SyntaxKind.GetAccessorDeclaration);
var returnFieldStatement = SyntaxFactory.ParseStatement($"return {backingFieldName};");
getter = getter
.WithBody(SyntaxFactory.Block(SyntaxFactory.SingletonList(returnFieldStatement)))
.WithSemicolonToken(default(SyntaxToken));
AccessorDeclarationSyntax setter = original.AccessorList.Accessors.FirstOrDefault(ad => ad.Kind() == SyntaxKind.SetAccessorDeclaration);
var setPropertyStatement = SyntaxFactory.ParseStatement($"{backingFieldName} = value;");
setter = setter
.WithBody(SyntaxFactory.Block(SyntaxFactory.SingletonList(setPropertyStatement)))
.WithSemicolonToken(default(SyntaxToken));
updated = updated
.WithAccessorList(SyntaxFactory.AccessorList(SyntaxFactory.List(new[] { getter, setter })))
.WithAdditionalAnnotations(Formatter.Annotation)
.WithAdditionalAnnotations(UpdatedPropertyAnnotation);
return updated;
}
示例4: 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);
}
示例5: TooManyAccessorsAsync
private async Task<Document> TooManyAccessorsAsync(Document document, PropertyDeclarationSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var allAccessors = declaration.AccessorList.Accessors.OfType<AccessorDeclarationSyntax>();
bool foundGetAccessor = false;
AccessorDeclarationSyntax accessorToKeep = null;
var accessorList = declaration.AccessorList;
foreach (AccessorDeclarationSyntax accessor in allAccessors)
{
var keyword = accessor.Keyword;
if (keyword.IsKind(SyntaxKind.GetKeyword) && !foundGetAccessor)
{
accessorToKeep = accessor;
foundGetAccessor = true;
}
else
{
accessorList = accessorList.RemoveNode(accessor, 0);
}
}
var block = SyntaxFactory.Block(new StatementSyntax[0]);
if (accessorToKeep == null)
{
accessorToKeep = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, block);
}
SyntaxList<SyntaxNode> accessorsToAdd = new SyntaxList<SyntaxNode>();
accessorsToAdd = accessorsToAdd.Add(accessorToKeep);
var newPropertyDeclaration = declaration.WithAccessorList(null);
newPropertyDeclaration = generator.AddAccessors(newPropertyDeclaration, accessorsToAdd) as PropertyDeclarationSyntax;
return await ReplaceNode(declaration, newPropertyDeclaration, document);
}
示例6: VisitPropertyDeclaration
/// <summary>
/// Normalizes the <paramref name="declaration" />.
/// </summary>
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax declaration)
{
// Nothing to do here for properties without 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;
var originalDeclaration = declaration;
var statements = AsStatementBody(propertySymbol.GetMethod, declaration.ExpressionBody.Expression);
var getter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, statements);
var accessorList = SyntaxFactory.AccessorList(SyntaxFactory.SingletonList(getter));
declaration = declaration.WithAccessorList(accessorList);
declaration = declaration.WithExpressionBody(null).WithSemicolonToken(default(SyntaxToken));
return declaration.EnsureLineCount(originalDeclaration);
}
示例7: CreateNewProperty
static PropertyDeclarationSyntax CreateNewProperty(PropertyDeclarationSyntax property)
{
// create new auto property
var accessorDeclList = new SyntaxList<AccessorDeclarationSyntax>().Add(SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))).Add(SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));
var newProperty = property.WithAccessorList(SyntaxFactory.AccessorList(accessorDeclList)).WithTrailingTrivia(property.GetTrailingTrivia()).WithAdditionalAnnotations(Formatter.Annotation);
return newProperty;
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:7,代码来源:ReplacePropertyWithBackingFieldWithAutoPropertyCodeRefactoringProvider.cs
示例8: CreateNewProperty
static PropertyDeclarationSyntax CreateNewProperty(PropertyDeclarationSyntax property)
{
var getter = property.AccessorList.Accessors.FirstOrDefault (a => a.IsKind (SyntaxKind.GetAccessorDeclaration));
var setter = property.AccessorList.Accessors.FirstOrDefault (a => a.IsKind (SyntaxKind.SetAccessorDeclaration));
// create new auto property
var accessorDeclList = new SyntaxList<AccessorDeclarationSyntax>().Add(SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithModifiers (getter.Modifiers).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)))
.Add(SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithModifiers (setter.Modifiers).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));
var newProperty = property.WithAccessorList(SyntaxFactory.AccessorList(accessorDeclList)).WithTrailingTrivia(property.GetTrailingTrivia()).WithAdditionalAnnotations(Formatter.Annotation);
return newProperty;
}
开发者ID:ceddlyburge,项目名称:RefactoringEssentials,代码行数:11,代码来源:ReplacePropertyWithBackingFieldWithAutoPropertyCodeRefactoringProvider.cs
示例9: ExpandProperty
private static PropertyDeclarationSyntax ExpandProperty(PropertyDeclarationSyntax property, string backingFieldName)
{
AccessorDeclarationSyntax getter, setter;
if (!ExpansionChecker.TryGetAccessors(property, out getter, out setter))
{
throw new ArgumentException();
}
if (getter.Body == null)
{
var returnFieldStatement = SyntaxFactory.ParseStatement(string.Format("return {0};", backingFieldName));
getter = getter
.WithBody(SyntaxFactory.Block(SyntaxFactory.SingletonList(returnFieldStatement)));
}
getter = getter
.WithSemicolonToken(default(SyntaxToken));
var setPropertyStatement = SyntaxFactory.ParseStatement(string.Format("SetProperty(ref {0}, value, \"{1}\");", backingFieldName, property.Identifier.ValueText));
setter = setter
.WithBody(SyntaxFactory.Block(SyntaxFactory.SingletonList(setPropertyStatement)))
.WithSemicolonToken(default(SyntaxToken));
var newProperty = property
.WithAccessorList(SyntaxFactory.AccessorList(SyntaxFactory.List(new[] { getter, setter })))
.WithAdditionalAnnotations(Formatter.Annotation);
return newProperty;
}
示例10: UseExpressionBodyIfDesired
private static PropertyDeclarationSyntax UseExpressionBodyIfDesired(
Workspace workspace, PropertyDeclarationSyntax declaration, ParseOptions options)
{
if (declaration.ExpressionBody == null)
{
var preferExpressionBody = workspace.Options.GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedProperties).Value;
if (preferExpressionBody)
{
if (declaration.Initializer == null)
{
var expressionBody = TryGetExpressionBody(declaration.AccessorList, options);
if (expressionBody != null)
{
declaration = declaration.WithAccessorList(null)
.WithExpressionBody(expressionBody)
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
}
}
}
}
return declaration;
}
示例11: HandlePropertyDeclaration
private async Task<Document> HandlePropertyDeclaration(PropertyDeclarationSyntax declaration, Document document, CancellationToken cancellationToken)
{
var returnStatement = SyntaxFactory.ReturnStatement(
returnKeyword: SyntaxFactory.Token(SyntaxKind.ReturnKeyword),
expression: declaration.ExpressionBody.Expression,
semicolonToken: declaration.SemicolonToken);
var accessorDeclaration = SyntaxFactory.AccessorDeclaration(
kind: SyntaxKind.GetAccessorDeclaration,
body: SyntaxFactory.Block(returnStatement));
var newDeclaration = declaration
.WithAccessorList(
SyntaxFactory.AccessorList(
SyntaxFactory.SingletonList(accessorDeclaration)))
.WithExpressionBody(null)
.WithSemicolonToken(default(SyntaxToken))
.WithAdditionalAnnotations(Formatter.Annotation);
var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = oldRoot.ReplaceNode(declaration, newDeclaration);
return document.WithSyntaxRoot(newRoot);
}
示例12: VisitPropertyDeclaration
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
return base.VisitPropertyDeclaration(
node.WithAccessorList(
SyntaxFactory.AccessorList(
SyntaxFactory.List<AccessorDeclarationSyntax>(
new[]
{
SyntaxFactory
.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
.WithBody(SyntaxFactory.Block(SyntaxFactory.List<StatementSyntax>(
new[]
{
SyntaxFactory.ReturnStatement(
SyntaxFactory.MemberAccessExpression(
kind: SyntaxKind.SimpleMemberAccessExpression,
expression: SyntaxFactory.IdentifierName("global::"+_symbol.ContainingType.ToDisplayString()),
name: SyntaxFactory.IdentifierName(_symbol.Name))
)
}
)
)),
SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
.WithBody(SyntaxFactory.Block(SyntaxFactory.List<StatementSyntax>(
new[]
{
SyntaxFactory.ParseStatement("global::"+_symbol.ContainingType.ToDisplayString()+"."+_symbol.Name+" = value;")
}
)
))
}.Where(ad =>
(_symbol.GetMethod != null && ad.IsKind(SyntaxKind.GetAccessorDeclaration)) ||
(_symbol.SetMethod != null && ad.IsKind(SyntaxKind.SetAccessorDeclaration)))
)
)));
}
示例13: CreateAutoProperty
private static PropertyDeclarationSyntax CreateAutoProperty(PropertyDeclarationSyntax property, VariableDeclaratorSyntax variableDeclarator,
IFieldSymbol fieldSymbol, IPropertySymbol propertySymbol)
{
var newProperty = property.WithAccessorList(SyntaxFactory.AccessorList(SyntaxFactory.List(new[] {
SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
})));
newProperty = variableDeclarator.Initializer == null ?
newProperty :
newProperty.WithInitializer(variableDeclarator.Initializer)
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
newProperty = CreatePropertyWithCorrectAccessibility(newProperty, fieldSymbol, propertySymbol);
newProperty = newProperty
.WithTriviaFrom(property)
.WithAdditionalAnnotations(Formatter.Annotation);
return newProperty;
}
示例14: PerformAction
Document PerformAction(Document document, SemanticModel model, SyntaxNode root, PropertyDeclarationSyntax propertyDeclaration, bool needsSetter)
{
AccessorDeclarationSyntax accessor = null;
PropertyDeclarationSyntax newProp = null;
if (needsSetter)
{
accessor = SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration);
var getter = propertyDeclaration.AccessorList.Accessors.FirstOrDefault(m => m.IsKind(SyntaxKind.GetAccessorDeclaration));
if (getter == null)
{
//get;
accessor = accessor.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
}
else
{
var getField = ReplacePropertyWithBackingFieldWithAutoPropertyCodeRefactoringProvider.ScanGetter(model, getter);
if (getField == null && getter.Body == null)
{
//get;
accessor = accessor.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)).WithTrailingTrivia(getter.GetTrailingTrivia());
}
else if (getField == null || getField.IsReadOnly)
{
//readonly or no field can be found
accessor = accessor.WithBody(GetNotImplementedBlock());
}
else
{
//now we add a 'field = value'.
accessor = accessor.WithBody(SyntaxFactory.Block(
SyntaxFactory.ExpressionStatement(
SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(getField.Name), SyntaxFactory.IdentifierName("value")))));
}
}
newProp = propertyDeclaration.WithAccessorList(propertyDeclaration.AccessorList.AddAccessors(accessor));
}
else
{
accessor = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
var setter = propertyDeclaration.AccessorList.Accessors.FirstOrDefault(m => m.IsKind(SyntaxKind.SetAccessorDeclaration));
var accessorDeclList = new SyntaxList<AccessorDeclarationSyntax>();
if (setter == null)
{
//set;
accessor = accessor.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
}
else
{
var setField = ReplacePropertyWithBackingFieldWithAutoPropertyCodeRefactoringProvider.ScanSetter(model, setter);
if (setField == null && setter.Body == null)
{
//set;
accessor = accessor.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)).WithTrailingTrivia(setter.GetTrailingTrivia());
}
else if (setField == null)
{
//no field can be found
accessor = accessor.WithBody(GetNotImplementedBlock());
}
else
{
//now we add a 'return field;'.
accessor = accessor.WithBody(SyntaxFactory.Block(
SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(setField.Name))));
}
accessorDeclList = accessorDeclList.Add(propertyDeclaration.AccessorList.Accessors.First(m => m.IsKind(SyntaxKind.SetAccessorDeclaration)));
}
accessorDeclList = accessorDeclList.Insert(0, accessor);
var accessorList = SyntaxFactory.AccessorList(accessorDeclList);
newProp = propertyDeclaration.WithAccessorList(accessorList);
}
var newRoot = root.ReplaceNode((SyntaxNode)propertyDeclaration, newProp).WithAdditionalAnnotations(Formatter.Annotation);
return document.WithSyntaxRoot(newRoot);
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:76,代码来源:AddAnotherAccessorCodeRefactoringProvider.cs
示例15: VisitPropertyDeclaration
/// <summary>
/// Normalizes the <paramref name="declaration" />.
/// </summary>
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax declaration)
{
var propertySymbol = declaration.GetPropertySymbol(SemanticModel);
if (!propertySymbol.ContainingType.IsComponent(SemanticModel) || !propertySymbol.IsExtern)
return declaration;
var originalDeclaration = declaration;
var index = declaration.Modifiers.IndexOf(SyntaxKind.ExternKeyword);
declaration = declaration.WithModifiers(declaration.Modifiers.RemoveAt(index)).WithSemicolonToken(default(SyntaxToken));
var accessors = SyntaxFactory.List(NormalizerAccessors(originalDeclaration.AccessorList.Accessors));
return declaration.WithAccessorList(declaration.AccessorList.WithAccessors(accessors)).EnsureLineCount(originalDeclaration);
}