Dynamically creating classes with specific CodeAttributeDeclarations

75 views Asked by At

I have a need to have custom code attributes that output something like "DataType(DataType.Text)"

I'm currently attempting to use CodeAttributeDeclarations.

But, something like this adds extra parenthesis:

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

So, that code ^^^ outputs this:

[DataType(DataType.Text)()]

And, what I need would be this:

[DataType(DataType.Text)]
1

There are 1 answers

4
Chris Thompson On

This is based on @mjwills comment, but have you tried this:

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