2017-06-06 74 views
0

我写了一个函数将LocalDeclaration转换为Global Resources。现在,我与一个属性每个定义更换,但我想用新的语法与属性来代替它=>Roslyn(Lambda)表达式格式属性语法

public PropertyDeclarationSyntax ConvertToResourceProperty(string resouceClassIdentifier, string fieldName, string resourceKey, CSharpSyntaxNode field) 
    { 
     var stringType = SyntaxFactory.ParseTypeName("string"); 

     var resourceReturnIdentifier = SyntaxFactory.IdentifierName(resouceClassIdentifier + "." + resourceKey); 
     var returnResourceStatement = SyntaxFactory.ReturnStatement(resourceReturnIdentifier).NormalizeWhitespace(); 
     var getRescourceBlock = SyntaxFactory.Block(returnResourceStatement); 

     var getAccessor = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, getRescourceBlock).WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation); 

     var propertyDeclaration = SyntaxFactory.PropertyDeclaration(stringType, fieldName).AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)).NormalizeWhitespace(); 

     propertyDeclaration = propertyDeclaration.AddAccessorListAccessors(getAccessor).WithAdditionalAnnotations(Formatter.Annotation); 

     SyntaxTrivia[] leadingTrivia = field.GetLeadingTrivia().ToArray() ?? new[] { SyntaxFactory.Whitespace("\t") }; 
     return propertyDeclaration.WithTrailingTrivia(SyntaxFactory.Whitespace("\r\n")) 
        .WithLeadingTrivia(leadingTrivia) 
        .WithAdditionalAnnotations(Simplifier.Annotation); 
    } 

此代码创建一个属性,像这样:

public static LocalResourceName 
{ 
    get{ return Resources.LocalResourceName; } 
} 

我想它使财产如此:

public static LocalResourceName =>Resources.LocalResourceName; 

我不太确定什么会从syntaxfactory创建一个表达式属性?任何人都可以指出我正确的方法吗?

回答

0

在淘金上网之后,我找到了一个办法。为什么没有roslyn的文档?

public PropertyDeclarationSyntax ConvertToResourceProperty(string resouceClassIdentifier, string fieldName, string resourceKey, CSharpSyntaxNode field) 
{ 
    var stringType = SyntaxFactory.ParseTypeName("string"); 

    var resourceClassName = SyntaxFactory.IdentifierName(resouceClassIdentifier); 
    var resourceKeyName = SyntaxFactory.IdentifierName(resourceKey); 
    var memberaccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, resourceClassName, resourceKeyName); 

    var propertyLambda = SyntaxFactory.ArrowExpressionClause(memberaccess); 

    var propertyDeclaration = SyntaxFactory.PropertyDeclaration(new SyntaxList<AttributeListSyntax>(), new SyntaxTokenList(), 
                   stringType, null, SyntaxFactory.Identifier(fieldName), null, 
                   propertyLambda, null, SyntaxFactory.Token(SyntaxKind.SemicolonToken)) 
                     .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), 
                    SyntaxFactory.Token(SyntaxKind.StaticKeyword)).WithAdditionalAnnotations(Formatter.Annotation).NormalizeWhitespace(); 

    return propertyDeclaration.WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed) 
       .WithLeadingTrivia(field.GetLeadingTrivia().ToArray()) 
       .WithAdditionalAnnotations(Simplifier.Annotation); 
} 
+3

下面是搞清楚如何产生这些有用的资源:使用即时窗口是如此讨厌https://roslynquoter.azurewebsites.net/ – JoshVarty

+0

@JoshVarty该资源感谢 –