johnny 5
johnny 5

Reputation: 20995

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)
                     .NormalizeWhitespace();

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

Upvotes: 1

Views: 641

Answers (2)

Matt Warren
Matt Warren

Reputation: 2016

You can use the '|' operator to combine declaration modifiers:

DeclarationModifiers.Sealed | DeclarationModifiers.Abstract

just like with flag enums.

Upvotes: 1

George Alexandria
George Alexandria

Reputation: 2936

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

Upvotes: 1

Related Questions