CustomAttributeData.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. ConstructorInfo ctorInfo;
  44. IList<CustomAttributeTypedArgument> ctorArgs;
  45. IList<CustomAttributeNamedArgument> namedArgs;
  46. #if NET_4_0
  47. protected CustomAttributeData ()
  48. {
  49. }
  50. #endif
  51. internal CustomAttributeData (ConstructorInfo ctorInfo, object [] ctorArgs, object [] namedArgs)
  52. {
  53. this.ctorInfo = ctorInfo;
  54. this.ctorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>
  55. (ctorArgs != null ? UnboxValues<CustomAttributeTypedArgument> (ctorArgs) : new CustomAttributeTypedArgument [0]);
  56. this.namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument>
  57. (namedArgs != null ? UnboxValues<CustomAttributeNamedArgument> (namedArgs) : new CustomAttributeNamedArgument [0]);
  58. }
  59. [ComVisible (true)]
  60. public
  61. #if NET_4_0
  62. virtual
  63. #endif
  64. ConstructorInfo Constructor {
  65. get {
  66. return ctorInfo;
  67. }
  68. }
  69. [ComVisible (true)]
  70. public
  71. #if NET_4_0
  72. virtual
  73. #endif
  74. IList<CustomAttributeTypedArgument> ConstructorArguments {
  75. get {
  76. return ctorArgs;
  77. }
  78. }
  79. public
  80. #if NET_4_0
  81. virtual
  82. #endif
  83. IList<CustomAttributeNamedArgument> NamedArguments {
  84. get {
  85. return namedArgs;
  86. }
  87. }
  88. public static IList<CustomAttributeData> GetCustomAttributes (Assembly target) {
  89. return MonoCustomAttrs.GetCustomAttributesData (target);
  90. }
  91. public static IList<CustomAttributeData> GetCustomAttributes (MemberInfo target) {
  92. return MonoCustomAttrs.GetCustomAttributesData (target);
  93. }
  94. public static IList<CustomAttributeData> GetCustomAttributes (Module target) {
  95. return MonoCustomAttrs.GetCustomAttributesData (target);
  96. }
  97. public static IList<CustomAttributeData> GetCustomAttributes (ParameterInfo target) {
  98. return MonoCustomAttrs.GetCustomAttributesData (target);
  99. }
  100. public override string ToString ()
  101. {
  102. StringBuilder sb = new StringBuilder ();
  103. sb.Append ("[" + ctorInfo.DeclaringType.FullName + "(");
  104. for (int i = 0; i < ctorArgs.Count; i++) {
  105. sb.Append (ctorArgs [i].ToString ());
  106. if (i + 1 < ctorArgs.Count)
  107. sb.Append (", ");
  108. }
  109. if (namedArgs.Count > 0)
  110. sb.Append (", ");
  111. for (int j = 0; j < namedArgs.Count; j++) {
  112. sb.Append (namedArgs [j].ToString ());
  113. if (j + 1 < namedArgs.Count)
  114. sb.Append (", ");
  115. }
  116. sb.AppendFormat (")]");
  117. return sb.ToString ();
  118. }
  119. static T [] UnboxValues<T> (object [] values)
  120. {
  121. T [] retval = new T [values.Length];
  122. for (int i = 0; i < values.Length; i++)
  123. retval [i] = (T) values [i];
  124. return retval;
  125. }
  126. public override bool Equals (object obj)
  127. {
  128. CustomAttributeData other = obj as CustomAttributeData;
  129. if (other == null || other.ctorInfo != ctorInfo ||
  130. other.ctorArgs.Count != ctorArgs.Count ||
  131. other.namedArgs.Count != namedArgs.Count)
  132. return false;
  133. for (int i = 0; i < ctorArgs.Count; i++)
  134. if (ctorArgs [i].Equals (other.ctorArgs [i]))
  135. return false;
  136. for (int i = 0; i < namedArgs.Count; i++) {
  137. bool matched = false;
  138. for (int j = 0; j < other.namedArgs.Count; j++)
  139. if (namedArgs [i].Equals (other.namedArgs [j])) {
  140. matched = true;
  141. break;
  142. }
  143. if (!matched)
  144. return false;
  145. }
  146. return true;
  147. }
  148. public override int GetHashCode ()
  149. {
  150. int ret = ctorInfo.GetHashCode () << 16;
  151. // argument order-dependent
  152. for (int i = 0; i < ctorArgs.Count; i++)
  153. ret += ret ^ 7 + ctorArgs [i].GetHashCode () << (i * 4);
  154. // argument order-independent
  155. for (int i = 0; i < namedArgs.Count; i++)
  156. ret += (namedArgs [i].GetHashCode () << 5);
  157. return ret;
  158. }
  159. }
  160. }