Introduction to. Net Generation series (1): overview

Source: Internet
Author: User

Visual Studio is powerful because it can generate some code for developers as much as possible, eliminating the need to perform meaningless and mechanized operations on a regular basis, so that developers do not have to spend their time and energy on the details, so they can focus more on the core part of the program, and the development efficiency is greatly improved.

Speaking of code generation, it seems that the frequency of use is not high in daily development tasks, and dynamic compilation also has a performance consumption process.

So, do we need to know a little about code generation? It should be said that there is a need, maybe one day will be used. Sometimes, I will find that vs's code generation function may not meet my needs. At this time, I will also consider writing a code generation tool myself.

The. NET Class Library also provides support, although it cannot be said that all code can be generated, at least most of the commonly used code is no problem.

Is code generation hard to learn? Ha, it's a bit, but it's hard to say it's difficult. What's important is that the language basics must be well understood. You know how to write code to know how to generate code.

 

Let's take a look at an example to learn how to generate code.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. codedom; using system. codedom. compiler; namespace consoleapplication1 {class program {static void main (string [] ARGs) {// declare a namespace codenamespace mynamespace = new codenamespace ("MyApp "); // reference other namespaces. imports. addrange (New codenamespaceimport [] {New codenamespaceimport ("system"), new codenamespa Ceimport ("system. collections "), new codenamespaceimport (" system. data "), new codenamespaceimport (" system. text ")}); // declare a class codetypedeclaration myclass = new codetypedeclaration (" mytestclass "); // define the main entry point method codeentrypointmethod myentrypoint = new codeentrypointmethod (); // The main method should be public static myentrypoint. attributes = memberattributes. public | memberattributes. static; // The return type of the main method is void myentr. Ypoint. returntype = new codetypereference (typeof (void); // The Code myentrypoint in the main method. statements. add (New codemethodinvokeexpression (New codemethodreferenceexpression (New codetypereferenceexpression (typeof (console), "writeline"), new codeprimitiveexpression ("Hello, my application. "); // Add the method to the class myclass. members. add (myentrypoint); // Add the class to the namespace mynamespace. types. add (myclass); // create the code unit codecompileunit myunit = new codecompileunit (); // Add the namespace to the code unit myunit. namespaces. add (mynamespace); // create a code generation class-C # codedomprovider providercs = codedomprovider. createprovider ("C #"); // generate the code providercs. generatecodefromcompileunit (myunit, console. out, new codegeneratoroptions {bracingstyle = "C"}); // generate VB. net code codedomprovider providervb = codedomprovider. createprovider ("VB"); providervb. generatecodefromcompileunit (myunit, console. out, new codegeneratoroptions (); console. readkey ();}}}

Run this program. We can see two sections of code: the first section is the C # code, and the second section is the VB. NET code.

 

 

 

The above code may be a bit confusing, but don't worry. This example only gives you an overall understanding of code generation. In the following article, I will try my best to describe every knowledge point, and every key point will have a very simple example to help you understand.

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.