Roslyn(Lambda)表达身体属性语法

时间:2017-06-06 18:33:19

标签: c# lambda properties roslyn

我编写了一个将LocalDeclaration转换为Global Resources的函数。现在我用一个属性替换每个定义,但我想用一个属性替换它使用新的语法=>

from pandas import Series

此代码创建如下属性:

    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; }
}

我不太确定什么会从syntaxfactory创建一个表达身体的属性?谁能指出我正确的方法?

1 个答案:

答案 0 :(得分:1)

在搜索互联网后,我找到了办法。为什么没有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);
}