C # automatically generate classes

Source: Internet
Author: User
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 );}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.