摘要 class 未使用 CodeDom 生成

Abstract class not generating with CodeDom

我有这个class定义:

CodeTypeDeclaration helloWorldClass = new CodeTypeDeclaration("HelloWorld") {
     Attributes = MemberAttributes.Abstract | MemberAttributes.Public
};

为什么这个声明会生成一个非抽象class?

public class HelloWorld
{
}

您需要使用 CodeTypeDeclaration.TypeAttributes 而不是 MemberAttributes:

CodeTypeDeclaration helloWorldClass = new CodeTypeDeclaration("HelloWorld") 
{
     TypeAttributes = TypeAttributes.Abstract | TypeAttributes.Public
};

Why did they add a Attributes property if it does nothing when defining a type?

即在documentation中明确指定:

Some of the flags such as Abstract overlap with the meaning of flags in the Attributes property of CodeTypeDeclaration that is inherited from CodeTypeMember. The Attributes property is a side effect of the CodeTypeDeclaration class inheriting from CodeTypeMember so that classes can be nested. The flags in the TypeAttributes property should be used instead of the flags in the Attributes property.

为了使用这样的继承层次,他们做了一个轻微的混淆复制。这就是为什么好的文档很重要。