Using System. codeDom; using System. codeDom. compiler; using Microsoft. CSharp; using System. reflection; namespace Test {public partial class Coder {public static void CreateClassFile () {/* Note: import the following namespace using System first. codeDom using System. codeDom. compiler; using Microsoft. CSharp; using System. reflection; * // prepare a code compiler unit CodeCompileUnit = new CodeCompileUnit (); // prepare the necessary namespace (this refers to the space of the class to be generated) codeNamespace sampleNamespace = new CodeNamespace ("Xizhang.com"); // import the necessary namespace sampleNamespace. imports. add (new CodeNamespaceImport ("System"); // prepare the definition of the Class to be generated. CodeTypeDeclaration Customerclass = new CodeTypeDeclaration ("Customer"); // specify 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 unit in the namespace set of the compiler unit. namespaces. add (sampleNamespace); // This is the output file string outputFile = "Customer. cs "; // Add the field CodeMemberField = new CodeMemberField (typeof (System. string), "_ Id"); field. attributes = MemberAttributes. private; Customerclass. members. add (field); // Add the attribute 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 (new CodeCommentStatement ("this is the Id attribute"); 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 method (use CodeMemberMethod) -- here a little // Add Constructor (use CodeConstructor) -- here a little // Add the program entry point (use CodeEntryPointMethod) -- click here to add events (use CodeMemberEvent) -- click here to add features (use CodeAttributeDeclaration) Customerclass. customAttributes. add (new CodeAttributeDeclaration (new CodeTypeReference (typeof (SerializableAttribute); // 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 );}}}}