本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.PropertyDeclarationSyntax.IsEquivalentTo方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyDeclarationSyntax.IsEquivalentTo方法的具体用法?C# PropertyDeclarationSyntax.IsEquivalentTo怎么用?C# PropertyDeclarationSyntax.IsEquivalentTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.Syntax.PropertyDeclarationSyntax
的用法示例。
在下文中一共展示了PropertyDeclarationSyntax.IsEquivalentTo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitPropertyDeclaration
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax property)
{
// Check each node until we find the property in question. When we have it, we'll replace it with a Full Property
if (property.IsEquivalentTo(this._crunchedProperty))
{
AccessorDeclarationSyntax getter = null;
AccessorDeclarationSyntax setter = null;
SyntaxTokenList getterModifiers = default(SyntaxTokenList);
SyntaxTokenList setterModifiers = default(SyntaxTokenList);
bool hasGetter = false;
bool hasSetter = false;
if (property.IsExpressionProperty())
{
hasGetter = true;
}
else
{
hasGetter = property.HasGetter();
hasSetter = property.HasSetter();
if (hasGetter)
{
getterModifiers = property.GetGetter().Modifiers;
}
if (hasSetter)
{
setterModifiers = property.GetSetter().Modifiers;
}
}
if (hasGetter) // Check if original Auto Property had a getter
{
// Create a new Getter with a body, returning a private field
getter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithBody(
SyntaxFactory.Block(
SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(this.PrivateFieldName))) // return field e.g. return _myProperty;
)
.WithModifiers(getterModifiers) // Keep original modifiers
.WithoutTrivia();
}
if (hasSetter) // Check if original Auto Property had a setter
{
// Create a new Setter with a body, setter the private field
setter = SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithBody(
SyntaxFactory.Block(
SyntaxFactory.ExpressionStatement(
SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(this.PrivateFieldName), SyntaxFactory.IdentifierName("value")))) // Assignment e.g. _myProperty = value
)
.WithModifiers(setterModifiers) // Keep original modifiers
.WithoutTrivia();
}
// Create a new property. Set the type and name
var newProperty = SyntaxFactory.PropertyDeclaration(property.Type, property.Identifier.ValueText)
.WithModifiers(property.Modifiers); // use the modifier(s) of the original property
// Add getter and setter to accessor list
var accessors = new List<AccessorDeclarationSyntax>();
if (getter != null)
{
accessors.Add(getter);
}
if (setter != null)
{
accessors.Add(setter);
}
// Put together the property with our built up accessors list
newProperty = newProperty.WithAccessorList(
SyntaxFactory.AccessorList(
SyntaxFactory.List(accessors)));
return newProperty; // Returning our new property "replaces" the original
}
return base.VisitPropertyDeclaration(property);
}