AttributeCollection.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // System.ComponentModel.AttributeCollection.cs
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Reflection;
  33. using System.Runtime.InteropServices;
  34. namespace System.ComponentModel
  35. {
  36. [ComVisible (true)]
  37. public class AttributeCollection : ICollection, IEnumerable
  38. {
  39. private ArrayList attrList = new ArrayList ();
  40. public static readonly AttributeCollection Empty = new AttributeCollection ((ArrayList)null);
  41. internal AttributeCollection (ArrayList attributes)
  42. {
  43. attrList = attributes;
  44. }
  45. public AttributeCollection (Attribute[] attributes)
  46. {
  47. if (attributes != null)
  48. for (int i = 0; i < attributes.Length; i++)
  49. attrList.Add (attributes[i]);
  50. }
  51. public bool Contains (Attribute attr)
  52. {
  53. return attrList.Contains (attr);
  54. }
  55. public bool Contains (Attribute [] attributes)
  56. {
  57. if (attributes == null)
  58. return true;
  59. foreach (Attribute attr in attributes)
  60. if (!Contains (attr))
  61. return false;
  62. return true;
  63. }
  64. public void CopyTo (Array array, int index)
  65. {
  66. attrList.CopyTo (array, index);
  67. }
  68. public IEnumerator GetEnumerator ()
  69. {
  70. return attrList.GetEnumerator ();
  71. }
  72. public bool Matches (Attribute attr)
  73. {
  74. foreach (Attribute a in attrList)
  75. if (a.Match (attr))
  76. return true;
  77. return false;
  78. }
  79. public bool Matches (Attribute [] attributes)
  80. {
  81. foreach (Attribute a in attributes)
  82. if (!(Matches (a)))
  83. return false;
  84. return true;
  85. }
  86. protected Attribute GetDefaultAttribute (Type attributeType)
  87. {
  88. Attribute attr = null;
  89. BindingFlags bf = BindingFlags.Public | BindingFlags.Static;
  90. FieldInfo def = attributeType.GetField ("Default", bf);
  91. if (def == null) {
  92. ConstructorInfo constructorInfo = attributeType.GetConstructor (Type.EmptyTypes);
  93. if (constructorInfo != null)
  94. attr = constructorInfo.Invoke (null) as Attribute;
  95. if (attr != null && !attr.IsDefaultAttribute ())
  96. attr = null;
  97. } else {
  98. attr = (Attribute) def.GetValue (null);
  99. }
  100. return attr;
  101. }
  102. bool ICollection.IsSynchronized {
  103. get {
  104. return attrList.IsSynchronized;
  105. }
  106. }
  107. object ICollection.SyncRoot {
  108. get {
  109. return attrList.SyncRoot;
  110. }
  111. }
  112. public int Count {
  113. get {
  114. return attrList.Count;
  115. }
  116. }
  117. public virtual Attribute this[Type type] {
  118. get {
  119. Attribute attr = null;
  120. foreach (Attribute a in attrList) {
  121. if (type.IsAssignableFrom (a.GetType ())) {
  122. attr = a;
  123. break;
  124. }
  125. }
  126. if (attr == null)
  127. attr = GetDefaultAttribute (type);
  128. return attr;
  129. }
  130. }
  131. public virtual Attribute this[int index] {
  132. get {
  133. return (Attribute) attrList [index];
  134. }
  135. }
  136. }
  137. }