简体   繁体   中英

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?

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

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

DeclarationModifiers.Sealed | DeclarationModifiers.Abstract

just like with flag enums.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM