CustomAttributeData.cs 4.8 KB

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