In-depth introduction to. Net code generation series (3): namespace

Source: Internet
Author: User

After the previous article, I made a simple example. I don't know if you have any impressions. It doesn't matter if you're not impressed. Today we'll discuss how to generate a namespace.Code.

In fact, we are talking about the codenamespace class, which is used to generate the namespace definition code.

(1) imports attribute: a set of codenamespaceimport objects. What is the difference between the codenamespaceimport class and the codenamespace class? The codenamespace class generates the Code defined for the namespace. codenamespaceimport indicates the introduced namespace, that is, the using system that we often use in C.

(2) types attribute: contains a set of Type Definitions. As we all know, a namespace contains N classes, or enumeration, delegation, and other things.

(3) Comments attribute: codecommentstatement set, indicating the annotation, which is actually an annotation line starting.

 

Question 1: how to generate the namespace definition code

You can see the following example.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. codedom; using system. codedom. compiler; namespace MyApp {class program {static void main (string [] ARGs) {codenamespace myns = new codenamespace ("thetest"); codecompileunit myuint = new codecompileunit (); myuint. namespaces. add (myns); codedomprovider provider = codedomprovider. createprovider ("C #"); provider. generatecodefromcompileunit (myuint, console. out, new codegeneratoroptions (); console. readkey ();}}}

OK. Check the result.

 

 

Question 2: How to introduce a namespace

Extend the code above.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. codedom; using system. codedom. compiler; namespace MyApp {class program {static void main (string [] ARGs) {codenamespace myns = new codenamespace ("thetest"); codecompileunit myuint = new codecompileunit (); // introduce the namespace myns. imports. add (New codenamespaceimport ("system"); myns. imports. add (New codenamespaceimport ("system. data "); myns. imports. add (New codenamespaceimport ("system. drawing "); myuint. namespaces. add (myns); codedomprovider provider = codedomprovider. createprovider ("C #"); provider. generatecodefromcompileunit (myuint, console. out, new codegeneratoroptions (); console. readkey ();}}}

Check the results.

Question 3: how to add a comment for a namespace

Continue to expand based on the above Code.

 
// Add the annotation myns. comments. add (New codecommentstatement ("this is the generated code-namespace"); myns. comments. add (New codecommentstatement ("time:" + datetime. now. tostring (); myuint. namespaces. add (myns); codedomprovider provider = codedomprovider. createprovider ("C #"); provider. generatecodefromcompileunit (myuint, console. out, new codegeneratoroptions ());

The result is as follows:

 

Question 4: define the type for the namespace

In the above Code, define a class for the namespace.

...... // Define a class codetypedeclaration myclass = new codetypedeclaration ("car"); myclass. attributes = memberattributes. public; myclass. comments. add (New codecommentstatement ("this is a class"); myns. types. add (myclass); myuint. namespaces. add (myns); codedomprovider provider = codedomprovider. createprovider ("C #"); provider. generatecodefromcompileunit (myuint, console. out, new codegeneratoroptions ());

Running result.

 

The complete code in this example is as follows:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. codedom; using system. codedom. compiler; namespace MyApp {class program {static void main (string [] ARGs) {codenamespace myns = new codenamespace ("thetest"); codecompileunit myuint = new codecompileunit (); // introduce the namespace myns. imports. add (New codenamespaceimport ("system"); myns. imports. add (New codenamespaceimport ("system. data "); myns. imports. add (New codenamespaceimport ("system. drawing "); // Add the annotation myns. comments. add (New codecommentstatement ("this is the generated code-namespace"); myns. comments. add (New codecommentstatement ("time:" + datetime. now. tostring (); // defines a class codetypedeclaration myclass = new codetypedeclaration ("car"); myclass. attributes = memberattributes. public; myclass. comments. add (New codecommentstatement ("this is a class"); myns. types. add (myclass); myuint. namespaces. add (myns); codedomprovider provider = codedomprovider. createprovider ("C #"); provider. generatecodefromcompileunit (myuint, console. out, new codegeneratoroptions (); console. readkey ();}}}

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.