CustomAttributeData.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. public
  38. class CustomAttributeData {
  39. class LazyCAttrData {
  40. internal Assembly assembly;
  41. internal IntPtr data;
  42. internal uint data_length;
  43. }
  44. ConstructorInfo ctorInfo;
  45. IList<CustomAttributeTypedArgument> ctorArgs;
  46. IList<CustomAttributeNamedArgument> namedArgs;
  47. LazyCAttrData lazyData;
  48. protected CustomAttributeData ()
  49. {
  50. }
  51. internal CustomAttributeData (ConstructorInfo ctorInfo, Assembly assembly, IntPtr data, uint data_length)
  52. {
  53. this.ctorInfo = ctorInfo;
  54. this.lazyData = new LazyCAttrData ();
  55. this.lazyData.assembly = assembly;
  56. this.lazyData.data = data;
  57. this.lazyData.data_length = data_length;
  58. }
  59. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  60. static extern void ResolveArgumentsInternal (ConstructorInfo ctor, Assembly assembly, IntPtr data, uint data_length, out object[] ctorArgs, out object[] namedArgs);
  61. void ResolveArguments ()
  62. {
  63. object[] ctor_args, named_args;
  64. if (lazyData == null)
  65. return;
  66. ResolveArgumentsInternal (ctorInfo, lazyData.assembly, lazyData.data, lazyData.data_length, out ctor_args, out named_args);
  67. this.ctorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>
  68. (ctor_args != null ? UnboxValues<CustomAttributeTypedArgument> (ctor_args) : EmptyArray<CustomAttributeTypedArgument>.Value);
  69. this.namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument>
  70. (named_args != null ? UnboxValues<CustomAttributeNamedArgument> (named_args) : EmptyArray<CustomAttributeNamedArgument>.Value);
  71. lazyData = null;
  72. }
  73. [ComVisible (true)]
  74. public
  75. virtual
  76. ConstructorInfo Constructor {
  77. get {
  78. return ctorInfo;
  79. }
  80. }
  81. [ComVisible (true)]
  82. public
  83. virtual
  84. IList<CustomAttributeTypedArgument> ConstructorArguments {
  85. get {
  86. ResolveArguments ();
  87. return ctorArgs;
  88. }
  89. }
  90. public
  91. virtual
  92. IList<CustomAttributeNamedArgument> NamedArguments {
  93. get {
  94. ResolveArguments ();
  95. return namedArgs;
  96. }
  97. }
  98. public static IList<CustomAttributeData> GetCustomAttributes (Assembly target) {
  99. return MonoCustomAttrs.GetCustomAttributesData (target);
  100. }
  101. public static IList<CustomAttributeData> GetCustomAttributes (MemberInfo target) {
  102. return MonoCustomAttrs.GetCustomAttributesData (target);
  103. }
  104. internal static IList<CustomAttributeData> GetCustomAttributesInternal (RuntimeType target) {
  105. return MonoCustomAttrs.GetCustomAttributesData (target);
  106. }
  107. public static IList<CustomAttributeData> GetCustomAttributes (Module target) {
  108. return MonoCustomAttrs.GetCustomAttributesData (target);
  109. }
  110. public static IList<CustomAttributeData> GetCustomAttributes (ParameterInfo target) {
  111. return MonoCustomAttrs.GetCustomAttributesData (target);
  112. }
  113. public Type AttributeType {
  114. get { return ctorInfo.DeclaringType; }
  115. }
  116. public override string ToString ()
  117. {
  118. StringBuilder sb = new StringBuilder ();
  119. sb.Append ("[" + ctorInfo.DeclaringType.FullName + "(");
  120. for (int i = 0; i < ctorArgs.Count; i++) {
  121. sb.Append (ctorArgs [i].ToString ());
  122. if (i + 1 < ctorArgs.Count)
  123. sb.Append (", ");
  124. }
  125. if (namedArgs.Count > 0)
  126. sb.Append (", ");
  127. for (int j = 0; j < namedArgs.Count; j++) {
  128. sb.Append (namedArgs [j].ToString ());
  129. if (j + 1 < namedArgs.Count)
  130. sb.Append (", ");
  131. }
  132. sb.AppendFormat (")]");
  133. return sb.ToString ();
  134. }
  135. static T [] UnboxValues<T> (object [] values)
  136. {
  137. T [] retval = new T [values.Length];
  138. for (int i = 0; i < values.Length; i++)
  139. retval [i] = (T) values [i];
  140. return retval;
  141. }
  142. public override bool Equals (object obj)
  143. {
  144. CustomAttributeData other = obj as CustomAttributeData;
  145. if (other == null || other.ctorInfo != ctorInfo ||
  146. other.ctorArgs.Count != ctorArgs.Count ||
  147. other.namedArgs.Count != namedArgs.Count)
  148. return false;
  149. for (int i = 0; i < ctorArgs.Count; i++)
  150. if (ctorArgs [i].Equals (other.ctorArgs [i]))
  151. return false;
  152. for (int i = 0; i < namedArgs.Count; i++) {
  153. bool matched = false;
  154. for (int j = 0; j < other.namedArgs.Count; j++)
  155. if (namedArgs [i].Equals (other.namedArgs [j])) {
  156. matched = true;
  157. break;
  158. }
  159. if (!matched)
  160. return false;
  161. }
  162. return true;
  163. }
  164. public override int GetHashCode ()
  165. {
  166. int ret = ctorInfo == null ? 13 : (ctorInfo.GetHashCode () << 16);
  167. // argument order-dependent
  168. if (ctorArgs != null) {
  169. for (int i = 0; i < ctorArgs.Count; i++) {
  170. ret += ret ^ 7 + ctorArgs [i].GetHashCode () << (i * 4);
  171. }
  172. }
  173. // argument order-independent
  174. if (namedArgs != null) {
  175. for (int i = 0; i < namedArgs.Count; i++)
  176. ret += (namedArgs [i].GetHashCode () << 5);
  177. }
  178. return ret;
  179. }
  180. }
  181. }