使用特定的CodeAttributeDeclarations动态创建类

时间:2017-10-27 23:07:48

标签: c# custom-attributes dynamic-class-creation dynamic-class

我需要输出类似“DataType(DataType.Text)”的自定义代码属性

我目前正在尝试使用CodeAttributeDeclarations。

但是,这样的事情会增加额外的括号:

var cad = new CodeAttributeDeclaration("DataType(DataType.Text)");
newProperty.CustomAttributes.Add(cad);

所以,那个代码^^^输出这个:

[DataType(DataType.Text)()]

而且,我需要的是:

[DataType(DataType.Text)]

1 个答案:

答案 0 :(得分:0)

这是基于@mjwills的评论,但你试过这个:

var cad = new CodeAttributeDeclaration("DataType", new CodeAttributeArgument(new CodePrimitiveExpression(DataType.Text)));