使用codeDom ---namespace 动态的生成.net代码
Advantages of CodeDom Technology
The CodeDom namespaces in the .NET Framework provide these advantages:
- The CodeDom is based upon a single model for rendering source code. Therefore, source code may be generated for any language that supports the CodeDom specification.
- The CodeDom allows programs to be dynamically created, compiled, and executed at runtime.
- The CodeDom provides a language independent object model for representing the structure of source code in memory.
- Future releases of the CodeDom could translate source code files between languages a lot like graphic file converter programs do with graphics files today. For example, a VB.NET program could be represented by the CodeDom then translated into C# for another developer. Cool huh?
Limitations of CodeDom Technology
The CodeDom namespaces contain classes to conceptually represent most programming constructs. Examples of these include: declarations, statements, iterations, arrays, casts, comments, error handling, and others. However, there are limitations to the current implementation of the CodeDom which will remain until the CodeDom namespaces are updated by Microsoft. In the meanwhile, to represent constructs not presently supported in CodeDom you can use the "Snippet" classes. These classes allow you to insert literal lines of source code into a compile unit. The classes available for handling snippets include: the CodeSnippetCompileUnit, CodeSnippetExpression, CodeSnippetStatement, and CodeSnippetTypeMember classes. You can use the snippet classes as a generic "catch all" for implementing presently unsupported programming constructs in the CodeDom. As the CodeDom namespaces are enhanced by Microsoft, you should not need to rely as heavily upon the snippet classes. As with any new technology, there are limitations to its functionality - CodeDom technology is no exception. We will look at four useful programming constructs not presently supported by the CodeDom. There are many more examples of missing constructs unsupported by the current implementation of CodeDom, but these should give you an idea of the enhancements forthcoming in the CodeDom namespaces.
- Limitation #1: There is no support for aliasing a namespace.
namespace MyNamespace { public void MyClass { } } namespace MyNamespace2 { using MyAlias = MyNamespace.MyClass; public void MyClass2 : MyAlias { } }
- Limitation #2: There is no support for nested namespaces.
namespace OutterNamespace { namespace InnerNamespace { public void MyClass { } } }
- Limitation #3: There is no support for variable declaration lists.
using System; using System.CodeDom; using System.CodeDom.Compiler; namespace MyNamespace { public void MyClass { public void MyClass() { int i,j,k; bool blnYes, blnNo; } } }
- Limitation #4: There is no support for the "unsafe" modifier in C#.
using System; class MyUnsafeClass { unsafe static void MyPointerFn(int* ptr) { *p *= *p; { { unsafe public static void main() { //unsafe operation using address-of operator & int i = 10; MyPointerFn(&i); Console.WriteLine(i); }
System.CodeDom Namespace Overview
The System.CodeDom Namespace contains classes, interfaces and enumerations to represent source code trees in memory. Each source code tree is called a CompileUnit. CompileUnits are linked together to form a tree structure in memory representing the structure of the source code. Remember our discussion on trees? Each CodeDom class is analogous to a node in the source tree. Each CodeDom node contains data which is used by the ICodeGenerator Interface later on. The ICodeGenerator Interface is what receives the Compile Units and outputs source code in the designated language. Compile Units are the key to understanding how the CodeDom creates the internal representation of the source code in memory.
System.CodeDom Enumerations |
|
CodeBinaryOperatorType |
Specifies identifiers for supported binary operators. |
FieldDirection |
Specifies identifiers used to indicate the direction of parameter and argument declarations. |
MemberAttributes |
Specifies member attribute identifiers for class members. |
|
|
System.CodeDom Classes |
|
CodeArgumentReferenceExpression |
Represents a reference to an argument. |
CodeArrayCreateExpression |
Represents an expression that creates an array. |
CodeArrayIndexerExpression |
Represents an expression that indicates an array and a specific index or indices. |
CodeAssignStatement |
Represents a simple assignment statement. |
CodeAttachEventStatement |
Represents a statement that attaches an event handler. |
CodeAttributeArgument |
Represents an argument used in a metadata custom attribute declaration. |
CodeAttributeArgumentCollection |
Represents a collection of CodeAttributeArgument objects. |
CodeAttributeDeclaration |
Represents an attribute declaration. |
CodeAttributeDeclarationCollection |
Represents a collection of CodeAttributeDeclaration objects. |
CodeBaseReferenceExpression |
Represents a reference to the base class. |
CodeBinaryOperatorExpression |
Represents an expression that consists of a binary operation between two expressions. |
CodeCastExpression |
Represents an expression that is to be cast to a data type or interface. |
CodeCatchClause |
Represents a catch exception block. |
CodeCatchClauseCollection |
Represents a collection of CodeCatchClause objects. |
CodeComment |
Represents a comment. |
CodeCommentStatement |
Represents a statement consisting of a single comment. |
CodeCommentStatementCollection |
Represents a collection of CodeCommentStatement objects. |
CodeCompileUnit |
Provides a top-level object to use for compilation. |
CodeConditionStatement |
Represents a conditional branch statement, typically represented as an if statement. |
CodeConstructor |
Represents the declaration of an instance constructor for a type. |
CodeDelegateCreateExpression |
Represents an expression that creates a delegate. |
CodeDelegateInvokeExpression |
Represents an expression that invokes a delegate. |
CodeDirectionExpression |
Represents an expression that indicates the direction type of the reference. |
CodeEntryPointMethod |
Represents the entry point of an executable. |
CodeEventReferenceExpression |
Represents an expression that references an event. |
CodeExpression |
Represents a code expression. This is a base class for other code expression objects that is never instantiated. |
CodeExpressionCollection |
Represents a collection of CodeExpression objects. |
CodeExpressionStatement |
Represents a statement that consists of a single expression. |
CodeFieldReferenceExpression |
Represents a reference to a field. |
CodeGotoStatement |
Represents a goto statement. |
CodeIndexerExpression |
Represents a reference to an indexer property of an object. |
CodeIterationStatement |
Represents a for statement, or a simple loop through a block of statements, using a test expression as a condition for continuing to loop. |
CodeLabeledStatement |
Represents a labeled statement or a stand-alone label. |
CodeLinePragma |
Represents a specific location within a specific file. |
CodeMemberEvent |
Represents an event member of a class. |
CodeMemberField |
Represents a field class member declaration. |
CodeMemberMethod |
Represents a declaration for a method of a class. |
CodeMemberProperty |
Represents a declaration for a property of a class. |
CodeMethodInvokeExpression |
Represents an expression that invokes a method. |
CodeMethodReferenceExpression |
Represents an expression that references a method on a specific object. |
CodeMethodReturnStatement |
Represents a return statement. |
CodeNamespace |
Represents a namespace declaration. |
CodeNamespaceCollection |
Represents a collection of CodeNamespace objects. |
CodeNamespaceImport |
Represents a namespace import directive that indicates a namespace to use. |
CodeNamesapceImportCollection |
Represents a collection of CodeNamespaceImport objects. |
CodeObject |
Provides a common base class for most Code Document Object Model (CodeDOM) objects. |
CodeObjectCreateExpression |
Represents an expression that creates a new instance of an object. |
CodeParamaterDeclarationExpression |
Represents a parameter declaration for a method, property, or constructor. |
CodeParameterDeclarationExpressionCollection |
Represents a collection of CodeParameterDeclarationExpression objects. |
CodePrimitiveExpression |
Represents a primitive data type value. |
CodePropertyReferenceExpression |
Represents a reference to a property. |
CodePropertySetValueReferenceExpression |
Represents an expression that represents the value argument of a property set method call within a property set method declaration. |
CodeRemoveEventStatement |
Represents a statement that removes an event handler. |
CodeSnippetCompileUnit |
Represents a literal code fragment that can be compiled. |
CodeSnippetExpression |
Represents a literal expression. |
CodeSnippetStatement |
Represents a statement using a literal code fragment. |
CodeSnippetTypeMember |
Represents a member of a class using a literal code fragment. |
CodeStatementCollection |
Represents a collection ofCodeStatement objects. |
CodeThisReferenceExpression |
Represents a reference to the current local class instance. |
CodeThrowExceptionStatement |
Represents a statement that throws an exception. |
CodeTryCatchFinallyStatement |
Represents a try block, with any number of catch clauses and optionally, a finally block. |
CodeTypeConstructor |
Represents a static constructor for a class. |
CodeTypeDeclaration |
Represents a type declaration for a class, structure, interface or enumeration. |
CodeTypeDeclarationCollection |
Represents a collection of CodeTypeDeclaration objects. |
CodeTypeDelegate |
Represents a delegate declaration. |
CodeTypeMember |
Represents the declaration for a member of a type. Type members include fields, methods, properties, constructors and nested types. |
CodeTypeMemberCollection |
Represents a collection of CodeTypeMember objects. |
CodeTypeOfExpression |
Represents a typeof expression, an expression that returns a specified runtime type. |
CodeTypeReference |
Represents a data type to CodeDOM objects. |
CodeTypeReferenceCollection |
Represents a collection of CodeTypeReference objects. |
CodeTypeReferenceExpression |
Represents a reference to a data type. |
CodeVariableDeclarationStatement |
Represents a declaration of a variable. |
CodeVariableReferenceExpression |
Represents an expression that references a local variable. |
System.CodeDom.Compiler Namespace Overview
The System.CodeDom.Compiler namespace contains enumerations, interfaces and classes used to generate and compile source code. Compile Units created using the System.CodeDom namespace are collected and processed by the System.CodeDom.Compiler namespace. When generating source code, CompileUnits are processed by three main interfaces: the ICodeParser, ICodeGenerator, and ICodeCompiler Interfaces. The ICodeParser Interface parses the CompileUnit trees into a structure in memory. The ICodeGenerator Interface reads the output of the ICodeParser Interface and physically generates the source code files in the desired language. The ICodeCompiler Interface receives source code files as input and compiles the source code into assemblies. The key to understanding the System.CodeDom.Compiler namespace is to learn how the three exposed interfaces work: the ICodeParser, ICodeCompiler, and ICodeCompiler Interfaces. In short, the System.CodeDom.Compiler namespace operates on the premise of having tree structures called Compile Units already built and ready to be parsed, generated and compiled.
System.CodeDom.Compiler Enumerations |
|
GeneratorSupport |
Specifies identifiers used to determine whether a code generator supports certain types of code. |
LanguageOptions |
Specifies identifiers that indicate special features of a language. |
|
|
System.CodeDom.Compiler Interfaces |
|
ICodeCompiler |
Provides a compiler execution interface. |
ICodeGenerator |
Provides an interface for generating code. |
ICodeParser |
Specifies an interface for parsing code into a CodeCompileUnit. |
|
|
System.CodeDom.Compiler Classes |
|
CodeCompiler |
Provides a helper class for implementing an ICodeCompiler. |
CodeDomProvider |
Provides a base class for CodeDomProvider implementations. This class is abstract. |
CodeGenerator |
Provides a helper class for implementing an ICodeGenerator. This class is abstract. |
CodeGeneratorOptions |
Represents options used by code generators. |
CodeParser |
Provides a helper class for implementing an ICodeParser. |
CompilerError |
Represents a compiler error or warning. |
CompilerErrorCollection |
Represents a collection of CompilerError objects. |
CompilerParameters |
Represents the parameters used to invoke the compiler. |
CompilerResults |
Represents the results of compilation that are returned from the compiler. |
Executor |
Provides command execution functions for invoking compilers. This class cannot be inherited. |
IndentedTextWriter |
Provides a text writer that can indent new lines by a tab string token. |
TempFileCollection |
Represents a collection of temporary files. |