New tests, update.
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil / SecurityDeclaration.cs
1 //
2 // SecurityDeclaration.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2005 Jb Evain
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 namespace Mono.Cecil {
30
31         using System;
32         using System.Collections;
33         using System.Security;
34
35         public sealed class SecurityDeclaration : IRequireResolving, IAnnotationProvider, IReflectionVisitable {
36
37                 SecurityAction m_action;
38                 SecurityDeclarationReader m_reader;
39                 IDictionary m_annotations;
40
41 #if !CF_1_0 && !CF_2_0
42                 PermissionSet m_permSet;
43 #endif
44
45                 bool m_resolved;
46                 byte [] m_blob;
47
48                 public SecurityAction Action {
49                         get { return m_action; }
50                         set { m_action = value; }
51                 }
52
53 #if !CF_1_0 && !CF_2_0
54                 public PermissionSet PermissionSet {
55                         get { return m_permSet; }
56                         set { m_permSet = value; }
57                 }
58 #endif
59
60                 public bool Resolved {
61                         get { return m_resolved; }
62                         set { m_resolved = value; }
63                 }
64
65                 public byte [] Blob {
66                         get { return m_blob; }
67                         set { m_blob = value; }
68                 }
69
70                 IDictionary IAnnotationProvider.Annotations {
71                         get {
72                                 if (m_annotations == null)
73                                         m_annotations = new Hashtable ();
74                                 return m_annotations;
75                         }
76                 }
77
78                 public SecurityDeclaration (SecurityAction action)
79                 {
80                         m_action = action;
81                 }
82
83                 internal SecurityDeclaration (SecurityAction action, SecurityDeclarationReader reader)
84                 {
85                         m_action = action;
86                         m_reader = reader;
87                 }
88
89                 public SecurityDeclaration Clone ()
90                 {
91                         return Clone (this);
92                 }
93
94                 internal static SecurityDeclaration Clone (SecurityDeclaration sec)
95                 {
96                         SecurityDeclaration sd = new SecurityDeclaration (sec.Action);
97                         if (!sec.Resolved) {
98                                 sd.Resolved = false;
99                                 sd.Blob = sec.Blob;
100                                 return sd;
101                         }
102
103 #if !CF_1_0 && !CF_2_0
104             sd.PermissionSet = sec.PermissionSet.Copy ();
105 #endif
106                         return sd;
107                 }
108
109                 public bool Resolve ()
110                 {
111                         if (m_resolved)
112                                 return true;
113
114                         if (m_reader == null)
115                                 return false;
116
117                         SecurityDeclaration clone = m_reader.FromByteArray (m_action, m_blob, true);
118                         if (!clone.Resolved)
119                                 return false;
120
121                         m_action = clone.Action;
122 #if !CF_1_0 && !CF_2_0
123                         m_permSet = clone.PermissionSet.Copy ();
124 #endif
125                         m_resolved = true;
126
127                         return true;
128                 }
129
130                 public void Accept (IReflectionVisitor visitor)
131                 {
132                         visitor.VisitSecurityDeclaration (this);
133                 }
134         }
135 }
136