I want to implement interfaces in my code generator, so I need to convert Microsoft.CodeAnalysis.Accessibility
(i.e. from ISymbol.DeclaredAccessibility
) to their represented modifier keywords.
This enum is used in code analysis APIs for describing access modifiers of a class or its members.
For instance, if you have public void MyMember()
its ISymbol.DeclaredAccessibility
will be Accessibility.Public
.
I need to create such a method declaration, thus I need a way to convert Accessibility.Public
to public
and so on (take note that just ToLower
will work only for simple cases, it won't generate protected internal
).
What is the correct way to do it?
With the SyntaxFacts.GetText(Accessibility) Method:
string publicKeyword = SyntaxFacts.GetText(Accessibility.Public);
string accessibilityKeyword = SyntaxFacts.GetText(accessibility);
Namespace: Microsoft.CodeAnalysis.CSharp
Assembly: Microsoft.CodeAnalysis.CSharp.dll
Package: Microsoft.CodeAnalysis.CSharp
Available since: v3.0.0
[Theory]
[InlineData(Accessibility.NotApplicable, "")]
[InlineData(Accessibility.Private, "private")]
[InlineData(Accessibility.ProtectedAndInternal, "private protected")]
[InlineData(Accessibility.Protected, "protected")]
[InlineData(Accessibility.Internal, "internal")]
[InlineData(Accessibility.ProtectedOrInternal, "protected internal")]
[InlineData(Accessibility.Public, "public")]
public void Accessibility_To_ModifierKeyword(Accessibility accessibility, string expected)
{
string actual = SyntaxFacts.GetText(accessibility);
actual.Should().Be(expected);
}