1 //
2 // TypeDefinitionCollection.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // Generated by /CodeGen/cecil-gen.rb do not edit
8 // Fri Mar 30 18:43:56 +0200 2007
9 //
10 // (C) 2005 Jb Evain
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 
32 namespace Mono.Cecil {
33 
34 	using System;
35 	using System.Collections;
36 	using System.Collections.Specialized;
37 
38 	using Mono.Cecil.Cil;
39 
40 	using Hcp = Mono.Cecil.HashCodeProvider;
41 	using Cmp = System.Collections.Comparer;
42 
43 	internal sealed class TypeDefinitionCollection : NameObjectCollectionBase, IList, IReflectionVisitable  {
44 
45 		ModuleDefinition m_container;
46 
47 		public TypeDefinition this [int index] {
48 			get { return this.BaseGet (index) as TypeDefinition; }
49 			set { this.BaseSet (index, value); }
50 		}
51 
52 		public TypeDefinition this [string fullName] {
53 			get { return this.BaseGet (fullName) as TypeDefinition; }
54 			set { this.BaseSet (fullName, value); }
55 		}
56 
57 		public ModuleDefinition Container {
58 			get { return m_container; }
59 		}
60 
61 		public bool IsSynchronized {
62 			get { return false; }
63 		}
64 
65 		public object SyncRoot {
66 			get { return this; }
67 		}
68 
69 		bool IList.IsReadOnly {
70 			get { return false; }
71 		}
72 
73 		bool IList.IsFixedSize {
74 			get { return false; }
75 		}
76 
77 		object IList.this [int index] {
78 			get { return BaseGet (index); }
79 			set {
80 				Check (value);
81 				BaseSet (index, value);
82 			}
83 		}
84 
TypeDefinitionCollection(ModuleDefinition container)85 		public TypeDefinitionCollection (ModuleDefinition container) :
86 			base (Hcp.Instance, Cmp.Default)
87 		{
88 			m_container = container;
89 		}
90 
Add(TypeDefinition value)91 		public void Add (TypeDefinition value)
92 		{
93 			if (value == null)
94 				throw new ArgumentNullException ("value");
95 
96 			Attach (value);
97 
98 			this.BaseAdd (value.FullName, value);
99 		}
100 
Clear()101 		public void Clear ()
102 		{
103 			foreach (TypeDefinition item in this)
104 				Detach (item);
105 
106 			this.BaseClear ();
107 		}
108 
Contains(TypeDefinition value)109 		public bool Contains (TypeDefinition value)
110 		{
111 			return Contains (value.FullName);
112 		}
113 
Contains(string fullName)114 		public bool Contains (string fullName)
115 		{
116 			return this.BaseGet (fullName) != null;
117 		}
118 
IndexOf(TypeDefinition value)119 		public int IndexOf (TypeDefinition value)
120 		{
121 			string [] keys = this.BaseGetAllKeys ();
122 			return Array.IndexOf (keys, value.FullName, 0, keys.Length);
123 		}
124 
Remove(TypeDefinition value)125 		public void Remove (TypeDefinition value)
126 		{
127 			this.BaseRemove (value.FullName);
128 
129 			Detach (value);
130 		}
131 
RemoveAt(int index)132 		public void RemoveAt (int index)
133 		{
134 			TypeDefinition item = this [index];
135 			Remove (item);
136 
137 			Detach (item);
138 		}
139 
CopyTo(Array ary, int index)140 		public void CopyTo (Array ary, int index)
141 		{
142 			this.BaseGetAllValues ().CopyTo (ary, index);
143 		}
144 
GetEnumerator()145 		public new IEnumerator GetEnumerator ()
146 		{
147 			return this.BaseGetAllValues ().GetEnumerator ();
148 		}
149 
Accept(IReflectionVisitor visitor)150 		public void Accept (IReflectionVisitor visitor)
151 		{
152 			visitor.VisitTypeDefinitionCollection (this);
153 		}
154 
155 #if CF_1_0 || CF_2_0
BaseGetAllValues()156 		internal object [] BaseGetAllValues ()
157 		{
158 			object [] values = new object [this.Count];
159 			for (int i=0; i < values.Length; ++i) {
160 				values [i] = this.BaseGet (i);
161 			}
162 			return values;
163 		}
164 #endif
165 
Check(object value)166 		void Check (object value)
167 		{
168 			if (!(value is TypeDefinition))
169 				throw new ArgumentException ();
170 		}
171 
IList.Add(object value)172 		int IList.Add (object value)
173 		{
174 			Check (value);
175 			Add (value as TypeDefinition);
176 			return 0;
177 		}
178 
IList.Contains(object value)179 		bool IList.Contains (object value)
180 		{
181 			Check (value);
182 			return Contains (value as TypeDefinition);
183 		}
184 
IList.IndexOf(object value)185 		int IList.IndexOf (object value)
186 		{
187 			throw new NotSupportedException ();
188 		}
189 
IList.Insert(int index, object value)190 		void IList.Insert (int index, object value)
191 		{
192 			throw new NotSupportedException ();
193 		}
194 
IList.Remove(object value)195 		void IList.Remove (object value)
196 		{
197 			Check (value);
198 			Remove (value as TypeDefinition);
199 		}
200 
Detach(TypeReference type)201 		void Detach (TypeReference type)
202 		{
203 			type.Module = null;
204 		}
205 
Attach(TypeReference type)206 		void Attach (TypeReference type)
207 		{
208 			if (type.Module != null)
209 				throw new ReflectionException ("Type is already attached, clone it instead");
210 
211 			type.Module = m_container;
212 			type.AttachToScope (m_container);
213 
214 		}
215 	}
216 }
217