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)]
1

1 Answer 1

0

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

var cad = new CodeAttributeDeclaration("DataType", new CodeAttributeArgument(new CodePrimitiveExpression(DataType.Text)));
Sign up to request clarification or add additional context in comments.

4 Comments

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")]
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")) });
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.
These are the only two usings I had to import to get it all to compile: using System.CodeDom; using System.ComponentModel.DataAnnotations;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.