AttributeCollection.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. using System;
  11. using System.Collections;
  12. using System.Reflection;
  13. namespace System.ComponentModel
  14. {
  15. public class AttributeCollection : ICollection, IEnumerable
  16. {
  17. private ArrayList attrList = new ArrayList ();
  18. public static readonly AttributeCollection Empty = new AttributeCollection (null);
  19. public AttributeCollection (Attribute[] attributes)
  20. {
  21. if (attributes != null)
  22. for (int i = 0; i < attributes.Length; i++)
  23. attrList.Add (attributes[i]);
  24. }
  25. public bool Contains (Attribute attr)
  26. {
  27. return attrList.Contains (attr);
  28. }
  29. public bool Contains (Attribute [] attributes)
  30. {
  31. if (attributes == null)
  32. return true;
  33. foreach (Attribute attr in attributes)
  34. if (!Contains (attr))
  35. return false;
  36. return true;
  37. }
  38. public void CopyTo (Array array, int index)
  39. {
  40. attrList.CopyTo (array, index);
  41. }
  42. public IEnumerator GetEnumerator ()
  43. {
  44. return attrList.GetEnumerator ();
  45. }
  46. public bool Matches (Attribute attr)
  47. {
  48. foreach (Attribute a in attrList)
  49. if (a.Match (attr))
  50. return true;
  51. return false;
  52. }
  53. public bool Matches (Attribute [] attributes)
  54. {
  55. foreach (Attribute a in attributes)
  56. if (!(Matches (a)))
  57. return false;
  58. return true;
  59. }
  60. protected Attribute GetDefaultAttribute (Type attributeType)
  61. {
  62. Attribute attr;
  63. BindingFlags bf = BindingFlags.Public | BindingFlags.Static;
  64. FieldInfo def = attributeType.GetField ("Default", bf);
  65. if (def == null) {
  66. attr = Activator.CreateInstance (attributeType) as Attribute;
  67. if (attr != null && !attr.IsDefaultAttribute ())
  68. attr = null;
  69. } else {
  70. attr = (Attribute) def.GetValue (null);
  71. }
  72. return attr;
  73. }
  74. public bool IsSynchronized {
  75. get {
  76. return attrList.IsSynchronized;
  77. }
  78. }
  79. public object SyncRoot {
  80. get {
  81. return attrList.SyncRoot;
  82. }
  83. }
  84. public int Count {
  85. get {
  86. return attrList.Count;
  87. }
  88. }
  89. public virtual Attribute this[Type type] {
  90. get {
  91. Attribute attr = null;
  92. foreach (Attribute a in attrList) {
  93. if (a.GetType () == type){
  94. attr = a;
  95. break;
  96. }
  97. }
  98. if (attr == null)
  99. attr = GetDefaultAttribute (type);
  100. return attr;
  101. }
  102. }
  103. public virtual Attribute this[int index] {
  104. get {
  105. return (Attribute) attrList [index];
  106. }
  107. }
  108. }
  109. }