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