2 // SecurityDeclarationCollection.cs
5 // Jb Evain (jbevain@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace Mono.Cecil {
32 using System.Collections;
34 internal sealed class SecurityDeclarationCollection : IReflectionVisitable, IEnumerable {
37 IHasSecurity m_container;
39 public SecurityDeclaration this [int index] {
40 get { return m_items [index] as SecurityDeclaration; }
41 set { m_items [index] = value; }
44 public SecurityDeclaration this [SecurityAction action] {
45 get { return m_items [action] as SecurityDeclaration; }
46 set { m_items [action] = value; }
49 public IHasSecurity Container {
50 get { return m_container; }
54 get { return m_items.Count; }
57 public bool IsSynchronized {
61 public object SyncRoot {
65 public SecurityDeclarationCollection (IHasSecurity container)
67 m_container = container;
68 m_items = new Hashtable ();
71 public void Add (SecurityDeclaration value)
74 throw new ArgumentNullException ("value");
76 // Each action can only be added once so...
77 SecurityDeclaration current = (SecurityDeclaration) m_items[value.Action];
78 if (current != null) {
79 // ... further additions are transformed into unions
80 #if !CF_1_0 && !CF_2_0
81 current.PermissionSet = current.PermissionSet.Union (value.PermissionSet);
84 m_items.Add (value.Action, value);
85 SetHasSecurity (true);
92 SetHasSecurity (false);
95 public bool Contains (SecurityAction action)
97 return (m_items [action] != null);
100 public bool Contains (SecurityDeclaration value)
103 return (m_items.Count == 0);
105 SecurityDeclaration item = (SecurityDeclaration) m_items[value.Action];
109 #if !CF_1_0 && !CF_2_0
110 return value.PermissionSet.IsSubsetOf (item.PermissionSet);
112 // XXX For CF, this concept does not exist--so always be true
117 public void Remove (SecurityAction action)
119 m_items.Remove (action);
120 SetHasSecurity (this.Count > 0);
123 public void CopyTo (Array ary, int index)
125 m_items.Values.CopyTo (ary, index);
128 public IEnumerator GetEnumerator ()
130 return m_items.Values.GetEnumerator ();
133 public void Accept (IReflectionVisitor visitor)
135 visitor.VisitSecurityDeclarationCollection (this);
138 private void SetHasSecurity (bool value)
140 TypeDefinition td = (m_container as TypeDefinition);
143 td.Attributes |= TypeAttributes.HasSecurity;
145 td.Attributes &= ~TypeAttributes.HasSecurity;
148 MethodDefinition md = (m_container as MethodDefinition);
151 md.Attributes |= MethodAttributes.HasSecurity;
153 md.Attributes &= ~MethodAttributes.HasSecurity;