0

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)]
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
websculpt
  • 53
  • 9

1 Answers1

0

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

var cad = new CodeAttributeDeclaration("DataType", new CodeAttributeArgument(new CodePrimitiveExpression(DataType.Text)));
Chris Thompson
  • 490
  • 5
  • 17
  • That throws some red squiggly lines under the second "DataType.Text", so I had to wrap it in quotes for the code to build. But, it doesn't give me what I need -- it outputs this: [DataType("DataType.Text")] – websculpt Oct 28 '17 at 21:57
  • And, also this code doesn't work for me either: cad2 = new CodeAttributeDeclaration("DataType", new System.CodeDom.CodeAttributeArgument[] { new System.CodeDom.CodeAttributeArgument("DataType", new CodePrimitiveExpression("DataType.Text")) }); – websculpt Oct 28 '17 at 21:59
  • Perhaps you are missing some using statements for this code - it did compile for me, though I'm sure I had to import something, possibly multiple things for it. When I get home I can check on this for you. – Chris Thompson Oct 30 '17 at 14:30
  • These are the only two usings I had to import to get it all to compile: `using System.CodeDom;` `using System.ComponentModel.DataAnnotations;` – Chris Thompson Nov 01 '17 at 01:19