private void Generatecode () {
/* Note that the following namespaces are imported first
Using System.CodeDom
Using System.CodeDom.Compiler;
Using Microsoft.csharp;
Using System.Reflection;
*/
Preparing a code compiler unit
CodeCompileUnit unit = new CodeCompileUnit ();
Prepare the necessary namespaces (this refers to the space of the classes to be generated)
CodeNamespace samplenamespace=new CodeNamespace ("xizhang.com");
Import the required namespaces
SAMPLENAMESPACE.IMPORTS.ADD (New CodeNamespaceImport ("System"));
Prepare the definition of the class to be generated
CodeTypeDeclaration customerclass = new CodeTypeDeclaration ("Customer");
Specifies that this is a class
Customerclass.isclass = true;
Customerclass.typeattributes = Typeattributes.public | typeattributes.sealed;
Put this class under this namespace
SAMPLENAMESPACE.TYPES.ADD (Customerclass);
Add the namespace to the collection of namespaces in the compiler unit
Unit. Namespaces.add (Samplenamespace);
This is the output file
String outputFile = "Customer.cs";
Add Field
Codememberfield field = new Codememberfield (typeof (System.String), "_id");
Field. Attributes = memberattributes.private;
CUSTOMERCLASS.MEMBERS.ADD (field);
Add Property
Codememberproperty property = new Codememberproperty ();
Property. Attributes = Memberattributes.public | memberattributes.final;
Property. Name = "Id";
Property. Hasget = true;
Property. Hasset = true;
Property. Type = new CodeTypeReference (typeof (System.String));
PROPERTY.COMMENTS.ADD ("This is the id attribute") (new codecommentstatement);
Property. Getstatements.add (New Codemethodreturnstatement (New Codefieldreferenceexpression) (New Codethisreferenceexpression ( ), "_id"));
Property. Setstatements.add (New codeassignstatement new Codefieldreferenceexpression (New Codethisreferenceexpression (), "_Id "), new Codepropertysetvaluereferenceexpression ()));
Customerclass.Members.Add (property);
Add a method (using CodeMemberMethod)--Here
Add a constructor (using CodeConstructor)--Here's a little
Add Program entry point (using CodeEntryPointMethod)--Here
Add event (using Codememberevent)--Here's a little
Adding features (using codeattributedeclaration)
CUSTOMERCLASS.CUSTOMATTRIBUTES.ADD (New CodeAttributeDeclaration (New CodeTypeReference (typeof) ( SerializableAttribute)));
/* Generate enumeration, enum attribute description
Build attribute
CUSTOMERCLASS.CUSTOMATTRIBUTES.ADD (New CodeAttributeDeclaration (New CodeTypeReference (typeof) ( DescriptionAttribute)), New CodeAttributeArgument (New CodePrimitiveExpression (listitem.description)));
Specifies that this is an enum
Customerclass.isenum = true;
Customerclass.typeattributes = Typeattributes.public;
Add Field
Codememberfield field = new Codememberfield (typeof (Enum), "enumeration entry 1" + "=" + 1);
Field. Attributes = Memberattributes.public;
Build attribute
Field. Customattributes.add (New CodeAttributeDeclaration (New CodeTypeReference (typeof (DescriptionAttribute)), new CodeAttributeArgument (New CodePrimitiveExpression ("Enumeration description Information")));
CUSTOMERCLASS.MEMBERS.ADD (field);
**/
Generate code
CodeDomProvider Provider = Codedomprovider.createprovider ("CSharp");
CodeGeneratorOptions options = new CodeGeneratorOptions ();
Options. Bracingstyle = "C";
Options. Blanklinesbetweenmembers = true;
using (System.IO.StreamWriter SW = new System.IO.StreamWriter (outputFile)) {
Provider. Generatecodefromcompileunit (unit, SW, options);
}
}
The result of the final generation is
Note: This digest has been changed since http://www.cnblogs.com/chenxizhang/archive/2008/07/09/1238572.html
C # Dynamically generated class enumeration, etc.