2017-06-06 76 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該資源感謝 –