CustomAttribute.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // CustomAttribute.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 Mono.Collections.Generic;
  30. namespace Mono.Cecil {
  31. public struct CustomAttributeArgument {
  32. readonly TypeReference type;
  33. readonly object value;
  34. public TypeReference Type {
  35. get { return type; }
  36. }
  37. public object Value {
  38. get { return value; }
  39. }
  40. public CustomAttributeArgument (TypeReference type, object value)
  41. {
  42. Mixin.CheckType (type);
  43. this.type = type;
  44. this.value = value;
  45. }
  46. }
  47. public struct CustomAttributeNamedArgument {
  48. readonly string name;
  49. readonly CustomAttributeArgument argument;
  50. public string Name {
  51. get { return name; }
  52. }
  53. public CustomAttributeArgument Argument {
  54. get { return argument; }
  55. }
  56. public CustomAttributeNamedArgument (string name, CustomAttributeArgument argument)
  57. {
  58. Mixin.CheckName (name);
  59. this.name = name;
  60. this.argument = argument;
  61. }
  62. }
  63. public interface ICustomAttribute {
  64. TypeReference AttributeType { get; }
  65. bool HasFields { get; }
  66. bool HasProperties { get; }
  67. Collection<CustomAttributeNamedArgument> Fields { get; }
  68. Collection<CustomAttributeNamedArgument> Properties { get; }
  69. }
  70. public sealed class CustomAttribute : ICustomAttribute {
  71. readonly internal uint signature;
  72. internal bool resolved;
  73. MethodReference constructor;
  74. byte [] blob;
  75. internal Collection<CustomAttributeArgument> arguments;
  76. internal Collection<CustomAttributeNamedArgument> fields;
  77. internal Collection<CustomAttributeNamedArgument> properties;
  78. public MethodReference Constructor {
  79. get { return constructor; }
  80. set { constructor = value; }
  81. }
  82. public TypeReference AttributeType {
  83. get { return constructor.DeclaringType; }
  84. }
  85. public bool IsResolved {
  86. get { return resolved; }
  87. }
  88. public bool HasConstructorArguments {
  89. get {
  90. Resolve ();
  91. return !Mixin.IsNullOrEmpty (arguments);
  92. }
  93. }
  94. public Collection<CustomAttributeArgument> ConstructorArguments {
  95. get {
  96. Resolve ();
  97. return arguments ?? (arguments = new Collection<CustomAttributeArgument> ());
  98. }
  99. }
  100. public bool HasFields {
  101. get {
  102. Resolve ();
  103. return !Mixin.IsNullOrEmpty (fields);
  104. }
  105. }
  106. public Collection<CustomAttributeNamedArgument> Fields {
  107. get {
  108. Resolve ();
  109. return fields ?? (fields = new Collection<CustomAttributeNamedArgument> ());
  110. }
  111. }
  112. public bool HasProperties {
  113. get {
  114. Resolve ();
  115. return !Mixin.IsNullOrEmpty (properties);
  116. }
  117. }
  118. public Collection<CustomAttributeNamedArgument> Properties {
  119. get {
  120. Resolve ();
  121. return properties ?? (properties = new Collection<CustomAttributeNamedArgument> ());
  122. }
  123. }
  124. internal bool HasImage {
  125. get { return constructor != null && constructor.HasImage; }
  126. }
  127. internal ModuleDefinition Module {
  128. get { return constructor.Module; }
  129. }
  130. internal CustomAttribute (uint signature, MethodReference constructor)
  131. {
  132. this.signature = signature;
  133. this.constructor = constructor;
  134. this.resolved = false;
  135. }
  136. public CustomAttribute (MethodReference constructor)
  137. {
  138. this.constructor = constructor;
  139. this.resolved = true;
  140. }
  141. public CustomAttribute (MethodReference constructor, byte [] blob)
  142. {
  143. this.constructor = constructor;
  144. this.resolved = false;
  145. this.blob = blob;
  146. }
  147. public byte [] GetBlob ()
  148. {
  149. if (blob != null)
  150. return blob;
  151. if (!HasImage)
  152. throw new NotSupportedException ();
  153. return Module.Read (ref blob, this, (attribute, reader) => reader.ReadCustomAttributeBlob (attribute.signature));
  154. }
  155. void Resolve ()
  156. {
  157. if (resolved || !HasImage)
  158. return;
  159. Module.Read (this, (attribute, reader) => {
  160. try {
  161. reader.ReadCustomAttributeSignature (attribute);
  162. resolved = true;
  163. } catch (ResolutionException) {
  164. if (arguments != null)
  165. arguments.Clear ();
  166. if (fields != null)
  167. fields.Clear ();
  168. if (properties != null)
  169. properties.Clear ();
  170. resolved = false;
  171. }
  172. return this;
  173. });
  174. }
  175. }
  176. static partial class Mixin {
  177. public static void CheckName (string name)
  178. {
  179. if (name == null)
  180. throw new ArgumentNullException ("name");
  181. if (name.Length == 0)
  182. throw new ArgumentException ("Empty name");
  183. }
  184. }
  185. }