C # Dynamically generated class enumeration, etc.

Source: Internet
Author: User

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.

Related Article

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.