gpt4 book ai didi

c# - 不使用 CodeDom 生成的抽象类

转载 作者:行者123 更新时间:2023-11-30 21:57:58 25 4
gpt4 key购买 nike

我有这个类定义:

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

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

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.

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

关于c# - 不使用 CodeDom 生成的抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30372844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com