CustomAttributeData.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // System.Reflection/CustomAttributeData.cs
  3. //
  4. // Author:
  5. // Zoltan Varga ([email protected])
  6. // Carlos Alberto Cortez ([email protected])
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Runtime.CompilerServices;
  32. using System.Runtime.InteropServices;
  33. using System.Text;
  34. namespace System.Reflection {
  35. [ComVisible (true)]
  36. [Serializable]
  37. #if NET_4_0
  38. public
  39. #else
  40. public sealed
  41. #endif
  42. class CustomAttributeData {
  43. class LazyCAttrData {
  44. internal Assembly assembly;
  45. internal IntPtr data;
  46. internal uint data_length;
  47. }
  48. ConstructorInfo ctorInfo;
  49. IList<CustomAttributeTypedArgument> ctorArgs;
  50. IList<CustomAttributeNamedArgument> namedArgs;
  51. LazyCAttrData lazyData;
  52. #if NET_4_0
  53. protected CustomAttributeData ()
  54. {
  55. }
  56. #endif
  57. internal CustomAttributeData (ConstructorInfo ctorInfo, Assembly assembly, IntPtr data, uint data_length)
  58. {
  59. this.ctorInfo = ctorInfo;
  60. this.lazyData = new LazyCAttrData ();
  61. this.lazyData.assembly = assembly;
  62. this.lazyData.data = data;
  63. this.lazyData.data_length = data_length;
  64. }
  65. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  66. static extern void ResolveArgumentsInternal (ConstructorInfo ctor, Assembly assembly, IntPtr data, uint data_length, out object[] ctorArgs, out object[] namedArgs);
  67. void ResolveArguments ()
  68. {
  69. object[] ctor_args, named_args;
  70. if (lazyData == null)
  71. return;
  72. ResolveArgumentsInternal (ctorInfo, lazyData.assembly, lazyData.data, lazyData.data_length, out ctor_args, out named_args);
  73. this.ctorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>
  74. (ctor_args != null ? UnboxValues<CustomAttributeTypedArgument> (ctor_args) : new CustomAttributeTypedArgument [0]);
  75. this.namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument>
  76. (named_args != null ? UnboxValues<CustomAttributeNamedArgument> (named_args) : new CustomAttributeNamedArgument [0]);
  77. lazyData = null;
  78. }
  79. [ComVisible (true)]
  80. public
  81. #if NET_4_0
  82. virtual
  83. #endif
  84. ConstructorInfo Constructor {
  85. get {
  86. return ctorInfo;
  87. }
  88. }
  89. [ComVisible (true)]
  90. public
  91. #if NET_4_0
  92. virtual
  93. #endif
  94. IList<CustomAttributeTypedArgument> ConstructorArguments {
  95. get {
  96. ResolveArguments ();
  97. return ctorArgs;
  98. }
  99. }
  100. public
  101. #if NET_4_0
  102. virtual
  103. #endif
  104. IList<CustomAttributeNamedArgument> NamedArguments {
  105. get {
  106. ResolveArguments ();
  107. return namedArgs;
  108. }
  109. }
  110. public static IList<CustomAttributeData> GetCustomAttributes (Assembly target) {
  111. return MonoCustomAttrs.GetCustomAttributesData (target);
  112. }
  113. public static IList<CustomAttributeData> GetCustomAttributes (MemberInfo target) {
  114. return MonoCustomAttrs.GetCustomAttributesData (target);
  115. }
  116. public static IList<CustomAttributeData> GetCustomAttributes (Module target) {
  117. return MonoCustomAttrs.GetCustomAttributesData (target);
  118. }
  119. public static IList<CustomAttributeData> GetCustomAttributes (ParameterInfo target) {
  120. return MonoCustomAttrs.GetCustomAttributesData (target);
  121. }
  122. public override string ToString ()
  123. {
  124. StringBuilder sb = new StringBuilder ();
  125. sb.Append ("[" + ctorInfo.DeclaringType.FullName + "(");
  126. for (int i = 0; i < ctorArgs.Count; i++) {
  127. sb.Append (ctorArgs [i].ToString ());
  128. if (i + 1 < ctorArgs.Count)
  129. sb.Append (", ");
  130. }
  131. if (namedArgs.Count > 0)
  132. sb.Append (", ");
  133. for (int j = 0; j < namedArgs.Count; j++) {
  134. sb.Append (namedArgs [j].ToString ());
  135. if (j + 1 < namedArgs.Count)
  136. sb.Append (", ");
  137. }
  138. sb.AppendFormat (")]");
  139. return sb.ToString ();
  140. }
  141. static T [] UnboxValues<T> (object [] values)
  142. {
  143. T [] retval = new T [values.Length];
  144. for (int i = 0; i < values.Length; i++)
  145. retval [i] = (T) values [i];
  146. return retval;
  147. }
  148. public override bool Equals (object obj)
  149. {
  150. CustomAttributeData other = obj as CustomAttributeData;
  151. if (other == null || other.ctorInfo != ctorInfo ||
  152. other.ctorArgs.Count != ctorArgs.Count ||
  153. other.namedArgs.Count != namedArgs.Count)
  154. return false;
  155. for (int i = 0; i < ctorArgs.Count; i++)
  156. if (ctorArgs [i].Equals (other.ctorArgs [i]))
  157. return false;
  158. for (int i = 0; i < namedArgs.Count; i++) {
  159. bool matched = false;
  160. for (int j = 0; j < other.namedArgs.Count; j++)
  161. if (namedArgs [i].Equals (other.namedArgs [j])) {
  162. matched = true;
  163. break;
  164. }
  165. if (!matched)
  166. return false;
  167. }
  168. return true;
  169. }
  170. public override int GetHashCode ()
  171. {
  172. int ret = ctorInfo.GetHashCode () << 16;
  173. // argument order-dependent
  174. for (int i = 0; i < ctorArgs.Count; i++)
  175. ret += ret ^ 7 + ctorArgs [i].GetHashCode () << (i * 4);
  176. // argument order-independent
  177. for (int i = 0; i < namedArgs.Count; i++)
  178. ret += (namedArgs [i].GetHashCode () << 5);
  179. return ret;
  180. }
  181. }
  182. }