Document directory
In Microsoft's own code generation solution, we have two options: CodeDom and Text Template Transformation Toolkit (T4) templates. Similarly, we can use a simple String or StringBuilder to concatenate strings. However, this is good for simplicity, but it is complicated. In fact, in ASP. net mvc 3.0 has a more concise syntax template-Razor. We can also use it in our own code generation. I will give you a brief introduction to Razor Templating Engine, you will have the opportunity to write the Razor Demo later. Today's topic is not here, so I will not talk about it much.
I. Introduction
CodeDom: This class library appears in our. NET Framework 2.0 and is deeply used in our ASP. NET project. CodeDom focuses on the independence of a language, so that we can build a CodeDom Model Tree using the familiar language (c #, vb, etc.) to generate. the language code supported by the. NET platform. ASP. NET requires language independence.
T4: the T4 template appears as part of vs2008, which is close to ASP.. Net Language inserts dynamic code blocks of segment in the template, which can be like ASP or ASP. net is as simple and more relevant as codedom. It is more concise than codedom, but it does not have the abstraction at the language level and does not have the language independence, we must write different templates for the same function template in different languages, but in development, the C # template is sufficient, and more simplified, so it is favored by more people.
Ii. Code demo
Here we will use codedom and T4 to generate a simple code and output different greetings based on time, as shown below:
1: codedom code:
Code
Using System;
Using System. Text;
Using System. CodeDom;
Using System. IO;
Namespace razordemo
{
Public class codedomdemo
{
Public codecompileunit cratecodecompileunit ()
{
VaR testclass = new codetypedeclaration ("test ");
Var testMeth = new CodeMemberMethod ()
{
Attributes = MemberAttributes. Public | MemberAttributes. Final,
Name = "SayHello ",
};
Var param = new CodeParameterDeclarationExpression (new CodeTypeReference (typeof (string), "name ");
TestMeth. Parameters. Add (param );
TestClass. Members. Add (testMeth );
Var nowExpression
= New CodePropertyReferenceExpression (new CodeTypeReferenceExpression (typeof (DateTime), "Now"), "Hour ");
Var assmon = new CodeBinaryOperatorExpression (new CodePrimitiveExpression ("Good morning:"), CodeBinaryOperatorType. Add, new CodeArgumentReferenceExpression ("name "));
Var asslunch = new CodeBinaryOperatorExpression (new CodePrimitiveExpression ("Good afternoon:"), CodeBinaryOperatorType. Add, new CodeArgumentReferenceExpression ("name "));
Var assAfternoon = new CodeBinaryOperatorExpression (new CodePrimitiveExpression ("Good afternoon:"), CodeBinaryOperatorType. Add, new CodeArgumentReferenceExpression ("name "));
Var codecondition = new CodeConditionStatement (new CodeBinaryOperatorExpression (
NowExpression, CodeBinaryOperatorType. LessThanOrEqual, new CodePrimitiveExpression (10 )),
New CodeStatement [] {new CodeExpressionStatement (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console), "WriteLine", assmon)}, // true: if (DateTime. now <= 10)
// Else
New CodeStatement []
{
New CodeConditionStatement (new CodeBinaryOperatorExpression (nowExpression, CodeBinaryOperatorType. LessThanOrEqual, new CodePrimitiveExpression (14), // else if (DateTime. Now <= 14)
New CodeStatement [] {new CodeExpressionStatement (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console), "WriteLine", asslunch)}, // true
New CodeStatement [] {new CodeExpressionStatement (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console), "WriteLine", assAfternoon)}) // fasle
}
);
TestMeth. Statements. AddRange (
New CodeStatement [] {
Codecondition
});
VaR NS
= New codenamespace ("wolf ");
NS. Imports. Add (New codenamespaceimport ("system "));
NS. types. Add (testclass );
VaR util = new codecompileunit ();
Util. namespaces. Add (NS );
Return util;
}
Public string Genertor (string language)
{
StringBuilder sb = new StringBuilder ();
StringWriter sw = new StringWriter (sb );
System. CodeDom. Compiler. CodeDomProvider. CreateProvider (language). GenerateCodeFromCompileUnit (
This. CrateCodeCompileUnit (), sw, new System. CodeDom. Compiler. CodeGeneratorOptions ()
{
ElseOnClosing = true,
IndentString = ""
});
Sw. Close ();
Return sb. ToString ();
}
}
}
2: T4 Code:
Code
<# @ Template language = "C #" #>
Using System;
Namespace WolfT4 {
Public class Test {
Public void SayHello (string name ){
<# If (System. DateTime. Now. Hour <= 10) {#>
System. Console. WriteLine ("Good morning:" + name ));
<#} Else if (System. DateTime. Now. Hour <= 14) {#>
System. Console. WriteLine ("Good afternoon:" + name ));
<#} Else {#>
System. Console. WriteLine ("Good afternoon:" + name ));
<#}#>
}
}
}
Iii. Summary
Advantages of CodeDom:
1: Language abstraction and independence: it is a single-language development and multi-language generation method.
2:FrameworkSupported: As part of our. NET Framework, it is located under the system. codedom namespace. You do not need to reference Microsoft. visualstudio. texttemplating. dll like the T4 template.
T4 advantages:
1: more appropriate: we use language blocks similar to ASP and ASP. NET. Yes, our development is more appropriate, and the template method is more concise and fast.
2: maintainability: because it is based on files, unlike codedom compiling to DLL, We can modify and refactor the template file at any time.
In fact, I think that as long as language independence is not required and multiple languages are generated, T4 or razor templates are used.
Code Generation Technology:
1: codedom series Directories
2: codesmith template engine series-directory
3: razor templating Engine
Source: http://www.cnblogs.com/whitewolf/archive/2010/12/23/1914700.html