none
Codedom - Enum inside a Class

    Question

  • I am using Codedom to build class automatically. What I want to do is generate below code automatically

    public class MyEnums

    {

        public enum EnumTypes

       {

        [EnumValueData(Val = "ABCD")]

        ABCD,

       [EnumValueData(Val = "EFGH")]

       EFGH,

       [EnumValueData(Val = "IJKL")]

       IJKL

     }

    }

    Problem is i can directly generate enum or class, I am not able to get anything where in i can generate Enum inside a class.

    Any help on this will be greatly appreciated.

    Thanks,
    Het
    Friday, June 27, 2008 2:39 PM

Answers

  • You can use the following code:

    CodeCompileUnit ccu = new CodeCompileUnit();  
    CodeNamespace cns = new CodeNamespace();  
    ccu.Namespaces.Add(cns);  
     
    CodeTypeDeclaration ctdc = new CodeTypeDeclaration("MyEnums");  
    cns.Types.Add(ctdc);  
     
    CodeTypeDeclaration ctde = new CodeTypeDeclaration("EnumTypes");  
    ctdc.Members.Add(ctde);  
    ctde.IsEnum = true;  
     
    CodeTypeReference evda = new CodeTypeReference(typeof(EnumValueDataAttribute));  
    CodeMemberField cmf0 = new CodeMemberField();  
    ctde.Members.Add(cmf0);  
    cmf0.Name = "ABCD";  
    cmf0.InitExpression = new CodePrimitiveExpression(0);  
    cmf0.CustomAttributes.Add(new CodeAttributeDeclaration(evda,  
        new CodeAttributeArgument("Val"new CodePrimitiveExpression("ABCD"))));  
     
    CodeMemberField cmf1 = new CodeMemberField();  
    ctde.Members.Add(cmf1);  
    cmf1.Name = "EFGH";  
    cmf1.InitExpression = new CodePrimitiveExpression(1);  
    cmf1.CustomAttributes.Add(new CodeAttributeDeclaration(evda,   
        new CodeAttributeArgument("Val"new CodePrimitiveExpression("EFGH"))));  
     
    using (CodeDomProvider provider = new CSharpCodeProvider())  
    {  
        CodeGeneratorOptions options = new CodeGeneratorOptions();  
        using (StringWriter tw = new StringWriter())  
        {  
            provider.GenerateCodeFromCompileUnit(ccu, tw, options);  
            Console.WriteLine(tw.ToString());  
        }  

    To generate the code:

    public class MyEnums {  
          
        public enum EnumTypes {  
              
            [EnumValueDataAttribute(Val="ABCD")]  
            ABCD = 0,  
              
            [EnumValueDataAttribute(Val="EFGH")]  
            EFGH = 1,  
        }  

    Probably the key for you is to realize that the nested type can be added to the MembersCollection (Members property) of the containing CodeTypeDeclaration.

     
    • Marked as answer by Bruno Yu Tuesday, July 01, 2008 5:34 AM
    Friday, June 27, 2008 11:57 PM

All replies

  • You can use the following code:

    CodeCompileUnit ccu = new CodeCompileUnit();  
    CodeNamespace cns = new CodeNamespace();  
    ccu.Namespaces.Add(cns);  
     
    CodeTypeDeclaration ctdc = new CodeTypeDeclaration("MyEnums");  
    cns.Types.Add(ctdc);  
     
    CodeTypeDeclaration ctde = new CodeTypeDeclaration("EnumTypes");  
    ctdc.Members.Add(ctde);  
    ctde.IsEnum = true;  
     
    CodeTypeReference evda = new CodeTypeReference(typeof(EnumValueDataAttribute));  
    CodeMemberField cmf0 = new CodeMemberField();  
    ctde.Members.Add(cmf0);  
    cmf0.Name = "ABCD";  
    cmf0.InitExpression = new CodePrimitiveExpression(0);  
    cmf0.CustomAttributes.Add(new CodeAttributeDeclaration(evda,  
        new CodeAttributeArgument("Val"new CodePrimitiveExpression("ABCD"))));  
     
    CodeMemberField cmf1 = new CodeMemberField();  
    ctde.Members.Add(cmf1);  
    cmf1.Name = "EFGH";  
    cmf1.InitExpression = new CodePrimitiveExpression(1);  
    cmf1.CustomAttributes.Add(new CodeAttributeDeclaration(evda,   
        new CodeAttributeArgument("Val"new CodePrimitiveExpression("EFGH"))));  
     
    using (CodeDomProvider provider = new CSharpCodeProvider())  
    {  
        CodeGeneratorOptions options = new CodeGeneratorOptions();  
        using (StringWriter tw = new StringWriter())  
        {  
            provider.GenerateCodeFromCompileUnit(ccu, tw, options);  
            Console.WriteLine(tw.ToString());  
        }  

    To generate the code:

    public class MyEnums {  
          
        public enum EnumTypes {  
              
            [EnumValueDataAttribute(Val="ABCD")]  
            ABCD = 0,  
              
            [EnumValueDataAttribute(Val="EFGH")]  
            EFGH = 1,  
        }  

    Probably the key for you is to realize that the nested type can be added to the MembersCollection (Members property) of the containing CodeTypeDeclaration.

     
    • Marked as answer by Bruno Yu Tuesday, July 01, 2008 5:34 AM
    Friday, June 27, 2008 11:57 PM
  • Thanks this was very helpful and it worked !, I did few changes though included below 

    CodeMemberField cmfEnum = new CodeMemberField();

    cmfEnum.Name = "ABCD"; 

    CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration("EnumValueData");

    CodeAttributeArgument codeAttriArg = new CodeAttributeArgument("Val", new CodeSnippetExpression("ABCD"));

    codeAttrDecl.Arguments.Add(codeAttriArg);

    cmfEnum.CustomAttributes.Add(codeAttrDecl);

    ctdClass.Members.Add(cmfEnum);

    • Edited by Het Waghela Tuesday, July 01, 2008 3:20 PM including Has Code
    Tuesday, July 01, 2008 3:20 PM