【发布时间】:2014-12-02 19:30:22
【问题描述】:
我正在使用 CodeDom 生成结构,我试图通过自定义属性指示字段的显式布局。
我已经设法使用普通的CodeAttributeDeclaration 实例来做到这一点,但后来我注意到枚举 System.Reflection.TypeAttributes 包含一个名为 ExplicitLayout 的字段,这应该是我正在寻找的。我是这样用的:
CodeTypeDeclaration type = new CodeTypeDeclaration();
type.Name = "MyStructure";
type.IsStruct = true;
type.TypeAttributes = TypeAttributes.Public | TypeAttributes.ExplicitLayout;
但是这并没有生成我需要的StructLayout 属性,为什么?
【问题讨论】:
-
因为这不是决定一个类型是否真正具有该属性的因素。例如,struct 类型将有 Type.IsExplicitLayout 返回 true,但它实际上没有属性。它仅用于反射目的,而不是用于生成代码。
-
感谢@Hans,所以 TypeAttributes 枚举不适用于我正在寻找的东西,明白了。
标签: c# code-generation codedom