PageRenderTime 20ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/Mono.Cecil/MemberDefinitionCollection.cs

http://github.com/jbevain/cecil
C# | 74 lines | 49 code | 16 blank | 9 comment | 4 complexity | f5945aba6d4168f6913def8ddec4fcb7 MD5 | raw file
 1//
 2// Author:
 3//   Jb Evain (jbevain@gmail.com)
 4//
 5// Copyright (c) 2008 - 2015 Jb Evain
 6// Copyright (c) 2008 - 2011 Novell, Inc.
 7//
 8// Licensed under the MIT/X11 license.
 9//
10
11using System;
12
13using Mono.Collections.Generic;
14
15namespace Mono.Cecil {
16
17	sealed class MemberDefinitionCollection<T> : Collection<T> where T : IMemberDefinition {
18
19		TypeDefinition container;
20
21		internal MemberDefinitionCollection (TypeDefinition container)
22		{
23			this.container = container;
24		}
25
26		internal MemberDefinitionCollection (TypeDefinition container, int capacity)
27			: base (capacity)
28		{
29			this.container = container;
30		}
31
32		protected override void OnAdd (T item, int index)
33		{
34			Attach (item);
35		}
36
37		protected sealed override void OnSet (T item, int index)
38		{
39			Attach (item);
40		}
41
42		protected sealed override void OnInsert (T item, int index)
43		{
44			Attach (item);
45		}
46
47		protected sealed override void OnRemove (T item, int index)
48		{
49			Detach (item);
50		}
51
52		protected sealed override void OnClear ()
53		{
54			foreach (var definition in this)
55				Detach (definition);
56		}
57
58		void Attach (T element)
59		{
60			if (element.DeclaringType == container)
61				return;
62
63			if (element.DeclaringType != null)
64				throw new ArgumentException ("Member already attached");
65
66			element.DeclaringType = this.container;
67		}
68
69		static void Detach (T element)
70		{
71			element.DeclaringType = null;
72		}
73	}
74}