CustomAttributeData.cs 6.6 KB

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