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