johnny 5 johnny 5 - 1 year ago 66
C# Question

Roslyn how to create a class with multiple Declaration Modifiers

I'm creating a code generator. to simplify the issue i'm having, how can I generate a class with multiple declaration modifiers?

The class Generator only has a constructor for adding a single Declaration Modifier

this._syntaxGenerator = SyntaxGenerator.GetGenerator(workspace, LanguageNames.CSharp);
var classNode = this._syntaxGenerator.ClassDeclaration(classOptions.Name, null,
classOptions.InternalAccessModifier, DeclarationModifiers.Sealed)

Say I wanted to create a sealed partial class or something with multiple Declaration Modifiers how can I do that?

Answer Source

Just try to use the some existing modifiers and recreate a new using WithIs**. It looks like this:

var modifiers = DeclarationModifiers.Sealed.WithIsAbstract(true).WithIsStatic(true);

After that you only need to pass it into SyntaxGenerator.ClassDeclaration

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download