AssemblyDefinition.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // AssemblyDefinition.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using Mono.Collections.Generic;
  31. namespace Mono.Cecil
  32. {
  33. public sealed class AssemblyDefinition : ICustomAttributeProvider, ISecurityDeclarationProvider
  34. {
  35. AssemblyNameDefinition name;
  36. internal ModuleDefinition main_module;
  37. Collection<ModuleDefinition> modules;
  38. Collection<CustomAttribute> custom_attributes;
  39. Collection<SecurityDeclaration> security_declarations;
  40. public AssemblyNameDefinition Name
  41. {
  42. get { return name; }
  43. set { name = value; }
  44. }
  45. public string FullName
  46. {
  47. get { return name != null ? name.FullName : string.Empty; }
  48. }
  49. public MetadataToken MetadataToken
  50. {
  51. get { return new MetadataToken(TokenType.Assembly, 1); }
  52. set { }
  53. }
  54. public Collection<ModuleDefinition> Modules
  55. {
  56. get
  57. {
  58. if (modules != null)
  59. return modules;
  60. if (main_module.HasImage)
  61. return main_module.Read(ref modules, this, (_, reader) => reader.ReadModules());
  62. return modules = new Collection<ModuleDefinition>(1) { main_module };
  63. }
  64. }
  65. public ModuleDefinition MainModule
  66. {
  67. get { return main_module; }
  68. }
  69. public MethodDefinition EntryPoint
  70. {
  71. get { return main_module.EntryPoint; }
  72. set { main_module.EntryPoint = value; }
  73. }
  74. public bool HasCustomAttributes
  75. {
  76. get
  77. {
  78. if (custom_attributes != null)
  79. return custom_attributes.Count > 0;
  80. return Mixin.GetHasCustomAttributes(this, main_module);
  81. }
  82. }
  83. public Collection<CustomAttribute> CustomAttributes
  84. {
  85. get { return custom_attributes ?? (Mixin.GetCustomAttributes(this, ref custom_attributes, main_module)); }
  86. }
  87. public bool HasSecurityDeclarations
  88. {
  89. get
  90. {
  91. if (security_declarations != null)
  92. return security_declarations.Count > 0;
  93. return Mixin.GetHasSecurityDeclarations(this, main_module);
  94. }
  95. }
  96. public Collection<SecurityDeclaration> SecurityDeclarations
  97. {
  98. get { return security_declarations ?? (Mixin.GetSecurityDeclarations(this, ref security_declarations, main_module)); }
  99. }
  100. internal AssemblyDefinition()
  101. {
  102. }
  103. public static AssemblyDefinition ReadAssembly(string fileName)
  104. {
  105. return ReadAssembly(ModuleDefinition.ReadModule(fileName));
  106. }
  107. public static AssemblyDefinition ReadAssembly(string fileName, ReaderParameters parameters)
  108. {
  109. return ReadAssembly(ModuleDefinition.ReadModule(fileName, parameters));
  110. }
  111. public static AssemblyDefinition ReadAssembly(Stream stream)
  112. {
  113. return ReadAssembly(ModuleDefinition.ReadModule(stream));
  114. }
  115. public static AssemblyDefinition ReadAssembly(Stream stream, ReaderParameters parameters)
  116. {
  117. return ReadAssembly(ModuleDefinition.ReadModule(stream, parameters));
  118. }
  119. static AssemblyDefinition ReadAssembly(ModuleDefinition module)
  120. {
  121. var assembly = module.Assembly;
  122. if (assembly == null)
  123. throw new ArgumentException();
  124. return assembly;
  125. }
  126. public override string ToString()
  127. {
  128. return this.FullName;
  129. }
  130. }
  131. }