Describe CodeDom and codedom

Source: Internet
Author: User

Describe CodeDom and codedom

Using the. NET Framework, I found the inherent defects of CodeDom, and I felt quite uncomfortable.

I don't know what CodeDom is. Please refer to CodeDom _ Baidu encyclopedia here.

Here is a very comprehensive Chinese tutorial on CodeDom-casual classification-lichdr-blog Park

First of all, CodeDom has a very good idea. It regards code as a research object and uses objects to represent code, which provides convenience for dynamic code processing.

CodeDom has many types and is hard to remember. CodeDom is often assembled by feeling, but it is often confused when several similar classes are selected, especially CodeTypeReference and CodeTypeReferenceExpression, there is also a special CodeExpressionStatement. The MSDN comments are very abstract. Fortunately, some hardworking friends have sorted out the comments. Microsoft CodeDom model learning notes (all)-Sai tister-blog Park

This is the nth time I came into contact with CodeDom. I knew that CodeDom was very tedious to write, so this time I made a simple encapsulation, so it was much simpler to use.

// Create an object var NewDbContext = Code. var ("MyDbContext", "db", Code. new ("MyDbContext ")). value; var NewUser = Code. var ("User", "newUser", Code. new ("User ")). value; // call a method // db. users. add (newUser); var db_Users_Add = Code. var ("db "). property ("Users "). method ("Add", Code. var ("newUser ")). value; // db. saveChanges (); var db_SaveChanges = Code. var ("db "). method ("SaveChanges"); // if (! Db. saveChanges (...)) {...} else {...} var If = Code. if (db_SaveChanges.Not ()). then (Code. class ("Console "). method ("WriteLine", Code. constValue ("Register failed. ")). asStatement ()). else (Code. class ("Console "). method ("WriteLine", Code. constValue ("Register Success. ")). asStatement ()). value; var codes = new CodeStatementCollection (); codes. add (NewDbContext); codes. add (NewUser); codes. add (db_Users_Add); codes. add (If );

 

Previously, it was written as follows:

CodeMemberMethod method = new CodeMemberMethod (); // method declaration; method. name = "SayHello"; // method Name method. attributes = MemberAttributes. public | MemberAttributes. final; // attribute method. returnType = new CodeTypeReference (typeof (string); // return type method. statements. add (new CodeMethodReturnStatement (new CodePrimitiveExpression ("Hello from code! "); // Method body, only one return statement return" Hello from code! "; CodeEntryPointMethod main = new CodeEntryPointMethod (); // main method Mainmain. statements. add (new CodeVariableDeclarationStatement ("HelloWord", "hw", new CodeObjectCreateExpression ("HelloWord", new CodeExpression [] {}); // variable declaration: helloWord hw = new HelloWord (); CodeMethodInvokeExpression methodinvoke = new CodeMethodInvokeExpression (new CodeVariableReferenceExpression ("hw"), "SayHello", new CodeExpression [] {}); main. statements. add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression ("System. console ")," WriteLine ", methodinvoke); main. statements. add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression ("System. console ")," Read "); // two methods are called: System. console. writeLine (hw. sayHello (); CodeTypeDeclaration helloword = new CodeTypeDeclaration ("HelloWord"); // type Class declaration helloword. attributes = MemberAttributes. public; helloword. members. addRange (new CodeTypeMember [] {method, main}); // Add the method to clssCodeNamespace nspace = new CodeNamespace ("HelloDemo1"); // namespace declaration nspace. imports. add (new CodeNamespaceImport ("System"); // introduce the program namespace: using System; nspace. types. add (helloword );

 

The content does not match. Here, we only want to demonstrate the difference between encapsulation and non-encapsulation.

Unfortunately, it takes a long time to find the answer to some "small" questions. What's terrible is that there is no answer in the end,/crazy. Recently, I encountered the simplest problem, that is, the exclamation point (!) in C (!), I have been searching for it for a long time, and I have invited a friend from senior engineers to help me find it. No. Some people may say that Snippets can be used, but if in such an encapsulation: enter an Expression, package a layer of "non" into a new Expression to return, snippets can only cry and say that it cannot be done.
Let's take a look at this dynamic generation and compilation (9)-the limitations of CodeDOM-lichdr-blog Park are still quite limited.

Most importantly, CSharpCodeProvider. Parse is not implemented, and the Parse methods in all languages are not implemented. The hateful Microsoft does not provide a parser! After testing all codeproviders, the results are the same. This is definitely what Microsoft did,

 


I think that several research projects have stopped, and now I think every time it stops at CodeDom, the original source is here. So I looked at NRefacotry, which is very powerful, according to the document. I would like to thank sertist for guiding this good thing. I am going to learn NRefacotry. This is all in English. I despise Microsoft CodeDom again. It is not easy to do. It is half done and incomplete, and it breaks down its beautiful image in my heart.

This article marks the death of our good System. CodeDom.

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.