CustomAttributeData.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #if NET_2_0
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Runtime.CompilerServices;
  33. using System.Runtime.InteropServices;
  34. using System.Text;
  35. namespace System.Reflection {
  36. #if NET_2_0
  37. [ComVisible (true)]
  38. #endif
  39. public sealed class CustomAttributeData {
  40. ConstructorInfo ctorInfo;
  41. IList<CustomAttributeTypedArgument> ctorArgs;
  42. IList<CustomAttributeNamedArgument> namedArgs;
  43. internal CustomAttributeData (ConstructorInfo ctorInfo, object [] ctorArgs, object [] namedArgs)
  44. {
  45. this.ctorInfo = ctorInfo;
  46. this.ctorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>
  47. (ctorArgs != null ? UnboxValues<CustomAttributeTypedArgument> (ctorArgs) : new CustomAttributeTypedArgument [0]);
  48. this.namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument>
  49. (namedArgs != null ? UnboxValues<CustomAttributeNamedArgument> (namedArgs) : new CustomAttributeNamedArgument [0]);
  50. }
  51. [ComVisible (true)]
  52. public ConstructorInfo Constructor {
  53. get {
  54. return ctorInfo;
  55. }
  56. }
  57. public IList<CustomAttributeTypedArgument> ConstructorArguments {
  58. get {
  59. return ctorArgs;
  60. }
  61. }
  62. public IList<CustomAttributeNamedArgument> NamedArguments {
  63. get {
  64. return namedArgs;
  65. }
  66. }
  67. public static IList<CustomAttributeData> GetCustomAttributes (Assembly target) {
  68. return MonoCustomAttrs.GetCustomAttributesData (target);
  69. }
  70. public static IList<CustomAttributeData> GetCustomAttributes (MemberInfo target) {
  71. return MonoCustomAttrs.GetCustomAttributesData (target);
  72. }
  73. public static IList<CustomAttributeData> GetCustomAttributes (Module target) {
  74. return MonoCustomAttrs.GetCustomAttributesData (target);
  75. }
  76. public static IList<CustomAttributeData> GetCustomAttributes (ParameterInfo target) {
  77. return MonoCustomAttrs.GetCustomAttributesData (target);
  78. }
  79. public override string ToString ()
  80. {
  81. StringBuilder sb = new StringBuilder ();
  82. sb.Append ("[" + ctorInfo.DeclaringType.Name + " (");
  83. for (int i = 0; i < ctorArgs.Count; i++) {
  84. sb.Append (ctorArgs [i].ToString ());
  85. if (i + 1 < ctorArgs.Count)
  86. sb.Append (", ");
  87. }
  88. if (namedArgs.Count > 0)
  89. sb.Append (", ");
  90. for (int j = 0; j < namedArgs.Count; j++) {
  91. sb.Append (namedArgs [j].ToString ());
  92. if (j + 1 < namedArgs.Count)
  93. sb.Append (", ");
  94. }
  95. sb.AppendFormat (")]");
  96. return sb.ToString ();
  97. }
  98. static T [] UnboxValues<T> (object [] values)
  99. {
  100. T [] retval = new T [values.Length];
  101. for (int i = 0; i < values.Length; i++)
  102. retval [i] = (T) values [i];
  103. return retval;
  104. }
  105. }
  106. }
  107. #endif