Codedom? Custom Attributes
Not sure if this is the correct forum for this, if not, please point me in the right direction.
I am using CodeDom to generate a ViewModel class from a Model class ( both c#). Simple enough, but in this case I need to preserve any Custom Attributes that exist on the Model at both the Class and Propertiy level. I am using reflection on the Model class, and am able to get the custom attributes, but I am not sure of the best way to create the custom attribute on the Generated Class. In particular the parameters for the attribute.
As an example a [XmlElement ( ElementName="Node1" IsNullabe=false )] custom attribute on the Model class needs to be preserved and moved to the CodeDom generated class. It implies the addition of a using for Xml.Serialization. I can get that from the namespace of the Custom Attribute on the Model. The issue for me is getting the parms for the custom attribute correct. Any suggestions for a good way to do this? What version of the "CodeAttributeArgument" is the correct way to do this so that the proper syntax is preserved. Some parms need quotes,some dont. Some need enums specified, some dont.It's all so confusing for a simple mind like mine. <grin>
Thanks
Paul- Moved byKarel ZikmundMSFTMonday, November 02, 2009 12:11 AMCodeDom is part of BCL (From:Building Development and Diagnostic Tools for .Net)
Answers
- You can use the System.Reflection.CustomAttributeData class to retrieve attribute definitions for a type or member in a format that will allow you to reproduce them via CodeDom.
- Marked As Answer byJialiang Ge [MSFT]MSFT, ModeratorFriday, November 13, 2009 6:21 AM
All Replies
Hello Paul
To add custom attributes, you can use CodeAttributeArgument http://msdn.microsoft.com/en-us/library/system.codedom.codeattributeargument.aspx. For example,
// Declare a new type called Class1.
CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");// Use attributes to mark the class as serializable and obsolete.
CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration(
"System.Serializable");
class1.CustomAttributes.Add(codeAttrDecl);CodeAttributeArgument codeAttr = new CodeAttributeArgument(
new CodePrimitiveExpression("This class is obsolete."));
codeAttrDecl = new CodeAttributeDeclaration("System.Obsolete",
codeAttr);
class1.CustomAttributes.Add(codeAttrDecl);
It generates this class:
// A C# code generator produces the following source code for the preceeding example code:
// [System.Serializable()]
// [System.Obsolete("This class is obsolete.")]
// public class Class1 {
// }
Please let me know whether this works for you.
Regards,
Jialiang Ge
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Thanks for the reply.
I am able to generate Attributes, the problem I have is trying to figure out the best way to take a reflected custom attribute from a PropertyInfo and generatte the equivalent Custom Attribute through CodeCom.
For instance if the following Custom Attributes are in the reflected class:
[XmlElement ( ElementName="Node1" IsNullable=false )]
[Browsable(false)]
[MyAttribute( Parm1=customEnum.Value, parm2="StringValue", parm3=1 )]
Public Property1 ( get; set; )
What is the best way to enumerate them and generate the same attributes in a codedom generated class? Because some of the values need to have quotes, some of them need the Enum added as a using, and then the value specified properly on the parm list, and some just take the propertyInfo.GetValue(). I dont see an easy CodeCom way of doing this.
I am able to reflect and get the appropriate namespaces to add the needed "usings" fine, but the parms and the constructor parms ( [Browsable(false)] ) are my problem. - You can use the System.Reflection.CustomAttributeData class to retrieve attribute definitions for a type or member in a format that will allow you to reproduce them via CodeDom.
- Marked As Answer byJialiang Ge [MSFT]MSFT, ModeratorFriday, November 13, 2009 6:21 AM
- Thanks, Nicole!
Hello pmont
Could you please try Nicole's suggestion and let us know whether it works for you?
Regards,
Jialiang Ge
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


