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创建一个表达式属性?任何人都可以指出我正确的方法吗?
下面是搞清楚如何产生这些有用的资源:使用即时窗口是如此讨厌https://roslynquoter.azurewebsites.net/ – JoshVarty
@JoshVarty该资源感谢 –