Original: http://www.cnblogs.com/plancr/archive/2004/11/29/70339.html
Codedom (on)
Codedom is almost the same, can't write code written by the code every day, and if you want to think about how it is to achieve such functions? Just establish a CodeDom class, then you can generate the code we hope, and you can compile it.
MS's things don't have source code, but now you can use the good tools such as Reflector to rough explore how to dry it (of course, if IL is passing, you can use iLDASM directly). This is really good.
It is actually the two namespaces with system.codedom under the .NET Farmework under the .NET Farmework.
The "Code Document Object Model" is "Code Document Object Model". To know that this thing is actually very virtual, it is basically not practical. It's so much in it, starting from the most basic expression and statement, a little pile up, finally get a namespace or compileUnit class, inserted in the system.codedom this namespace, you will get new to New to get Some Object, and what Object is, it is a little bit of data in memory. We can't see it. Otherwise, you don't need to go to o / r mapping. In order to show it. The previous namespace lies in constructing, the latter namespace lies in performance. The construct is a shelf, polymerizes the various parts of the contained aggregation, connects to the connection, this little mystery is not, all don't go deep.
(Speaking of DOM, I think of the DOM in XML, that thing is much different from this, but the things inside XML are more complicated than Code.)
Open system.codedom.compiler this namespace, which is much less than system.codedom, but though, but it is really, it is very real, all of which are used.
The "Dynamic Generation and Compilation" mentioned before, there is three major interfaces here: iCodeCompiler, Icodegenerator, iCODEPARSER, and CodedOMPROVIDERs that create these interfaces. You can use the same CODEDOM to generate code of different programming languages and compile generation code, and the key to achieving such a function is actually in provider. CodeDomProvider is an abstract class. Any a program language under .NET If you want to realize dynamic generation and compilation, you must provide a corresponding provider. For example, CSharpcodeProvider, VB.NET is VBCodeProvider et al. (Delphi2005 also provides CODEDOM features, so it also has the corresponding provider).
The following is only C # as an example. CsharpcodeProvider This class is named space in Microsoft.csharp
First generation, see the GenerateCodeFromCompileUnit () method of the icodegenerator interface (other methods in the interface are called from here).
Abstract class Public Abstract Class CodeGenerator: Interface implementation is available in iCodeGenerator:
Void
ICodegenerator.generatecodeFromCompileunit (CodeCompileUnit E, TextWriter W, CodegeneratorOptions O) {... // Some abnormal processing. And set the corresponding parameters according to the two parameters of W, O ... this.GenerateCompileunit (E); ......}
In the same class
protected
Virtual
Void
GenerateCompileUnit (CodeCompileUnit E)
{This.generateCompileUnitstart (e); this.GenerateNameSpaces (e); this.GenerateCompileunited (E);}
Peeled a layer, from CompileUnit to Namespace.
protected
Void
GenerateNameSpaces (CodeCompileUnit E)
{ForeEach (CODENAMESPACE NAMESPACE1 IN E.NameSpaces) {(iCodegenerator) this) .GeneratecodefromNamespace (Namespace1, this.Output.innerwriter, this.Options);}}
Void
Icodegenerator.generatecodefromNamespace (Codenamespace E, Textwriter W, CodegeneratorOptions O)
{... this.generatenamespace (e); ......}
The next layer is peeled off and Type is exposed.
protected
Virtual
Void
GenerateNamespace (CODENAMESPACE E)
{This.GenerateCommentStatements (e.comments); this.GenenderNameSpaceStart (e); this.GenerationTenamespaceImports (E); this.GenerateTypes (e); this.GenerateTypes (e);
Of course, it is a matter of the member in Type, and the following is another.
In fact, it can also be thought of that the code is generated is based on the model provided by Codedom, and the decomposition of a layer is decomposed into a specific statement of a sentence, which is just the opposite of the process of the up-layer ADD when the CodeDom is generated.
Playing the Tai Chi Boxing Now, I haven't seen a statement that generates the program code! Let's take a look at the members of the codegenerator class, and there is a lot of generate methods, as long as you have in system.codedom There is a corresponding generate method (this is inevitable), but most of these are abstract methods. Because the specific code is to be varied due to language, the specific implementation of these abstraction methods is definitely in the corresponding provider.
There is an Internal Class Csharpcodegenerator: CodeCompiler, and CreateCompiler (), the CreateCompiler (), is the above INTERNAL class. See what CSHARPCODEGENERATOR is there? A pile of generate methods. That's right, it is it, generating a C # code process is done here.
Just click on how to make a code, the simple point of this creation of assignment statements: protected
Override
Void
GenerateAssIgnStatement (CodeassignState E)
{Base.GenerateExpression (E.EFT); Base.Output.write ("="); base.generateExpression (E.right); if (! This.forloophack) {base.output.writeline (";");} }
Generate left, write a "=" number, then generate the right, finally write one ";" (if the assignment statement in the For loop header is special, don't ";" What is OUTPUT? It is the properties of the TextWriter parameter setting setting that generatecodefromCompileUnit () started based on the interface method. Look at a few Generate methods, you can see a lot of Base.Output.write ().
I understand very well, the generate process is written from the top of the lower layer to the textwriter, and a string is written in TextWriter.
So if you want's dynamic program does not need to be flexible (with a few variables, add some of the conditions to determine the kind), and if you don't have multiple languages, you don't have to use codedom, or yourself to make a textwriter. Write an effort