【问题标题】:Abstract class not generating with CodeDom不使用 CodeDom 生成的抽象类
【发布时间】:2015-08-03 01:38:32
【问题描述】:

我有这个类定义:

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

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

public class HelloWorld
{
}

【问题讨论】:

  • 可能是TypeAttributes = TypeAttributes.Abstract ?
  • 就是这样!!如果在定义类型时什么都不做,他们为什么要添加 Attributes 属性?
  • @MatiCicero 因为它们是MemberAttributes,所以它们适用于成员,而不是类型。
  • 我认为Attributes 用于字段、属性、方法等。
  • @MatiCicero 因为类型可以是类的成员

标签: c# .net dynamic metaprogramming codedom


【解决方案1】:

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

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

如果什么都不做,他们为什么要添加一个 Attributes 属性? 定义类型?

这是在documentation 中明确指定的:

Abstract 等一些标志与标志的含义重叠 在继承的 CodeTypeDeclaration 的 Attributes 属性中 来自 CodeTypeMember。 Attributes 属性是 CodeTypeDeclaration 类继承自 CodeTypeMember 以便 类可以嵌套。 TypeAttributes 属性中的标志应该 用于代替 Attributes 属性中的标志。

为了使用这样的继承层次,他们做了些许混乱的重复。这就是为什么好的文档很重要。

【讨论】:

  • @MatiCicero 欢迎。很高兴这是有道理的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多