/Mono.Cecil/AssemblyDefinition.cs

http://github.com/jbevain/cecil · C# · 189 lines · 142 code · 38 blank · 9 comment · 19 complexity · c6a5e8babe58cd0c0920522a63959a4b MD5 · raw file

  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. using System.IO;
  12. using System.Threading;
  13. using Mono.Collections.Generic;
  14. namespace Mono.Cecil {
  15. public sealed class AssemblyDefinition : ICustomAttributeProvider, ISecurityDeclarationProvider, IDisposable {
  16. AssemblyNameDefinition name;
  17. internal ModuleDefinition main_module;
  18. Collection<ModuleDefinition> modules;
  19. Collection<CustomAttribute> custom_attributes;
  20. Collection<SecurityDeclaration> security_declarations;
  21. public AssemblyNameDefinition Name {
  22. get { return name; }
  23. set { name = value; }
  24. }
  25. public string FullName {
  26. get { return name != null ? name.FullName : string.Empty; }
  27. }
  28. public MetadataToken MetadataToken {
  29. get { return new MetadataToken (TokenType.Assembly, 1); }
  30. set { }
  31. }
  32. public Collection<ModuleDefinition> Modules {
  33. get {
  34. if (modules != null)
  35. return modules;
  36. if (main_module.HasImage)
  37. return main_module.Read (ref modules, this, (_, reader) => reader.ReadModules ());
  38. Interlocked.CompareExchange (ref modules, new Collection<ModuleDefinition> (1) { main_module }, null);
  39. return modules;
  40. }
  41. }
  42. public ModuleDefinition MainModule {
  43. get { return main_module; }
  44. }
  45. public MethodDefinition EntryPoint {
  46. get { return main_module.EntryPoint; }
  47. set { main_module.EntryPoint = value; }
  48. }
  49. public bool HasCustomAttributes {
  50. get {
  51. if (custom_attributes != null)
  52. return custom_attributes.Count > 0;
  53. return this.GetHasCustomAttributes (main_module);
  54. }
  55. }
  56. public Collection<CustomAttribute> CustomAttributes {
  57. get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, main_module)); }
  58. }
  59. public bool HasSecurityDeclarations {
  60. get {
  61. if (security_declarations != null)
  62. return security_declarations.Count > 0;
  63. return this.GetHasSecurityDeclarations (main_module);
  64. }
  65. }
  66. public Collection<SecurityDeclaration> SecurityDeclarations {
  67. get { return security_declarations ?? (this.GetSecurityDeclarations (ref security_declarations, main_module)); }
  68. }
  69. internal AssemblyDefinition ()
  70. {
  71. }
  72. public void Dispose ()
  73. {
  74. if (this.modules == null) {
  75. main_module.Dispose ();
  76. return;
  77. }
  78. var modules = this.Modules;
  79. for (int i = 0; i < modules.Count; i++)
  80. modules [i].Dispose ();
  81. }
  82. public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleKind kind)
  83. {
  84. return CreateAssembly (assemblyName, moduleName, new ModuleParameters { Kind = kind });
  85. }
  86. public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleParameters parameters)
  87. {
  88. if (assemblyName == null)
  89. throw new ArgumentNullException ("assemblyName");
  90. if (moduleName == null)
  91. throw new ArgumentNullException ("moduleName");
  92. Mixin.CheckParameters (parameters);
  93. if (parameters.Kind == ModuleKind.NetModule)
  94. throw new ArgumentException ("kind");
  95. var assembly = ModuleDefinition.CreateModule (moduleName, parameters).Assembly;
  96. assembly.Name = assemblyName;
  97. return assembly;
  98. }
  99. public static AssemblyDefinition ReadAssembly (string fileName)
  100. {
  101. return ReadAssembly (ModuleDefinition.ReadModule (fileName));
  102. }
  103. public static AssemblyDefinition ReadAssembly (string fileName, ReaderParameters parameters)
  104. {
  105. return ReadAssembly (ModuleDefinition.ReadModule (fileName, parameters));
  106. }
  107. public static AssemblyDefinition ReadAssembly (Stream stream)
  108. {
  109. return ReadAssembly (ModuleDefinition.ReadModule (stream));
  110. }
  111. public static AssemblyDefinition ReadAssembly (Stream stream, ReaderParameters parameters)
  112. {
  113. return ReadAssembly (ModuleDefinition.ReadModule (stream, parameters));
  114. }
  115. static AssemblyDefinition ReadAssembly (ModuleDefinition module)
  116. {
  117. var assembly = module.Assembly;
  118. if (assembly == null)
  119. throw new ArgumentException ();
  120. return assembly;
  121. }
  122. public void Write (string fileName)
  123. {
  124. Write (fileName, new WriterParameters ());
  125. }
  126. public void Write (string fileName, WriterParameters parameters)
  127. {
  128. main_module.Write (fileName, parameters);
  129. }
  130. public void Write ()
  131. {
  132. main_module.Write ();
  133. }
  134. public void Write (WriterParameters parameters)
  135. {
  136. main_module.Write (parameters);
  137. }
  138. public void Write (Stream stream)
  139. {
  140. Write (stream, new WriterParameters ());
  141. }
  142. public void Write (Stream stream, WriterParameters parameters)
  143. {
  144. main_module.Write (stream, parameters);
  145. }
  146. public override string ToString ()
  147. {
  148. return this.FullName;
  149. }
  150. }
  151. }