PageRenderTime 83ms CodeModel.GetById 75ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/Visual Studio 2008/CSEmitAssembly/ReadMe.txt

#
Plain Text | 99 lines | 71 code | 28 blank | 0 comment | 0 complexity | eb8d2fcdb48925057fdd324587181389 MD5 | raw file
 1========================================================================
 2    CONSOLE APPLICATION : CSEmitAssembly Project Overview
 3========================================================================
 4
 5/////////////////////////////////////////////////////////////////////////////
 6Use:
 7
 8Reflection provides objects (of type Type) that encapsulate assemblies, 
 9modules and types. It allows us to
10
111. Access attributes in your program's metadata.
122. Examine and instantiate types in an assembly.
133. Dynamically load and use types.
144. Emit new types at runtime.
15
16This example shows the use of 4. CSReflection demonstrates 2 and 3. 
17
18The System.Reflection.Emit namespace allows emitting metadata and Microsoft
19intermediate language (MSIL) at run time and optionally generate a portable
20executable (PE) file on disk. 
21
22CSEmitAssembly emits these two types and save them to an assembly on disk:
23
24public class ClassA {
25    // Fields
26    private ClassB classBField;
27    private String stringField;
28
29    // Methods
30    public void ClassAMethod()
31    { this.classBField.ClassBMethod(null); }
32
33    // Properties
34    public ClassB ClassBProperty {
35        get { return this.classBField; }
36        set { this.classBField = value; }
37    }
38}
39
40public class ClassB {
41    // Fields
42    private List<ClassA> classAList;
43    private ClassA classAField;
44
45    // Methods
46    public void ClassBMethod(List<ClassA> list) {
47        this.classAField.ClassAMethod();
48    }
49
50    // Properties
51    public List<ClassA> ClassAList {
52        get { return this.classAList; }
53        set { this.classAList.AddRange(value); }
54    }
55}
56
57
58/////////////////////////////////////////////////////////////////////////////
59Implementation:
60
61The ReflectionEmitLanguage Add-in of .NET Reflector 
62(http://reflectoraddins.codeplex.com/)
63translates the IL code of a given method into the C# code that would be 
64needed to generate the same IL code using System.Reflection.Emit. We can 
65first build a .NET assembly with the target types, then use the tool to 
66generate the System.Reflection.Emit codes that emits the same types.
67
681. Define the assembly and the module.
69(AppDomain.DefineDynamicAssembly, AssemblyBuilder.DefineDynamicModule)
70
712. Declare the types. (ModuleBuilder.DefineType)
72
733. Define the types' constructors, fields, properties, and methods.
74(TypeBuilder.DefineConstructor, TypeBuilder.DefineField, 
75TypeBuilder.DefineProperty, TypeBuilder.DefineMethod)
76
774. Create the types. (TypeBuilder.CreateType)
78
795. Save the assembly. (AssemblyBuilder.Save)
80
81
82/////////////////////////////////////////////////////////////////////////////
83References:
84
85Emitting Dynamic Methods and Assemblies
86http://msdn.microsoft.com/en-us/library/8ffc3x75.aspx
87
88System.Reflection.Emit Namespace
89http://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx
90
91Dynamic Assemblies using Reflection.Emit. Part II of II - Reflection.Emit
92By Piyush S Bhatnagar
93http://www.codeproject.com/KB/cs/DynamicCodeGeneration2.aspx
94
95ReflectionEmitLanguage 
96http://www.codeplex.com/reflectoraddins/Wiki/View.aspx?title=ReflectionEmitLanguage&referringTitle=Home
97
98
99/////////////////////////////////////////////////////////////////////////////