AttributeCollection.cs 2.5 KB

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