GenericParameter.cs: override Module properly
[mcs.git] / class / corlib / System.Security.Permissions / ReflectionPermission.cs
blobb26c566a3b9f5610b08507d493f9b0e775b4e735
1 //
2 // System.Security.Permissions.ReflectionPermission.cs
3 //
4 // Authors:
5 // Tim Coleman <tim@timcoleman.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // Copyright (C) 2002, Tim Coleman
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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.
32 using System.Globalization;
33 using System.Runtime.InteropServices;
35 namespace System.Security.Permissions {
37 [ComVisible (true)]
38 [Serializable]
39 public sealed class ReflectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
41 private const int version = 1;
43 ReflectionPermissionFlag flags;
46 public ReflectionPermission (PermissionState state)
48 if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
49 flags = ReflectionPermissionFlag.AllFlags;
50 else
51 flags = ReflectionPermissionFlag.NoFlags;
54 public ReflectionPermission (ReflectionPermissionFlag flag)
56 // reuse validation by the Flags property
57 Flags = flag;
61 public ReflectionPermissionFlag Flags {
62 get { return flags; }
63 set {
64 const ReflectionPermissionFlag all_flags = ReflectionPermissionFlag.AllFlags | ReflectionPermissionFlag.RestrictedMemberAccess;
66 if ((value & all_flags) != value) {
67 string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
68 throw new ArgumentException (msg, "ReflectionPermissionFlag");
71 flags = value;
76 public override IPermission Copy ()
78 return new ReflectionPermission (flags);
81 public override void FromXml (SecurityElement esd)
83 // General validation in CodeAccessPermission
84 CheckSecurityElement (esd, "esd", version, version);
85 // Note: we do not (yet) care about the return value
86 // as we only accept version 1 (min/max values)
88 if (IsUnrestricted (esd)) {
89 flags = ReflectionPermissionFlag.AllFlags;
91 else {
92 flags = ReflectionPermissionFlag.NoFlags;
93 string xmlFlags = (esd.Attributes ["Flags"] as string);
94 if (xmlFlags.IndexOf ("MemberAccess") >= 0)
95 flags |= ReflectionPermissionFlag.MemberAccess;
96 if (xmlFlags.IndexOf ("ReflectionEmit") >= 0)
97 flags |= ReflectionPermissionFlag.ReflectionEmit;
98 if (xmlFlags.IndexOf ("TypeInformation") >= 0)
99 flags |= ReflectionPermissionFlag.TypeInformation;
103 public override IPermission Intersect (IPermission target)
105 ReflectionPermission rp = Cast (target);
106 if (rp == null)
107 return null;
109 if (IsUnrestricted ()) {
110 if (rp.Flags == ReflectionPermissionFlag.NoFlags)
111 return null;
112 else
113 return rp.Copy ();
115 if (rp.IsUnrestricted ()) {
116 if (flags == ReflectionPermissionFlag.NoFlags)
117 return null;
118 else
119 return Copy ();
122 ReflectionPermission p = (ReflectionPermission) rp.Copy ();
123 p.Flags &= flags;
124 return ((p.Flags == ReflectionPermissionFlag.NoFlags) ? null : p);
127 public override bool IsSubsetOf (IPermission target)
129 ReflectionPermission rp = Cast (target);
130 if (rp == null)
131 return (flags == ReflectionPermissionFlag.NoFlags);
133 if (IsUnrestricted ())
134 return rp.IsUnrestricted ();
135 else if (rp.IsUnrestricted ())
136 return true;
138 return ((flags & rp.Flags) == flags);
141 public bool IsUnrestricted ()
143 return (flags == ReflectionPermissionFlag.AllFlags);
146 public override SecurityElement ToXml ()
148 SecurityElement se = Element (version);
149 if (IsUnrestricted ()) {
150 se.AddAttribute ("Unrestricted", "true");
152 else {
153 if (flags == ReflectionPermissionFlag.NoFlags)
154 se.AddAttribute ("Flags", "NoFlags");
155 else if ((flags & ReflectionPermissionFlag.AllFlags) == ReflectionPermissionFlag.AllFlags)
156 se.AddAttribute ("Flags", "AllFlags");
157 else {
158 string xmlFlags = "";
159 if ((flags & ReflectionPermissionFlag.MemberAccess) == ReflectionPermissionFlag.MemberAccess)
160 xmlFlags = "MemberAccess";
161 if ((flags & ReflectionPermissionFlag.ReflectionEmit) == ReflectionPermissionFlag.ReflectionEmit) {
162 if (xmlFlags.Length > 0)
163 xmlFlags += ", ";
164 xmlFlags += "ReflectionEmit";
166 if ((flags & ReflectionPermissionFlag.TypeInformation) == ReflectionPermissionFlag.TypeInformation) {
167 if (xmlFlags.Length > 0)
168 xmlFlags += ", ";
169 xmlFlags += "TypeInformation";
171 se.AddAttribute ("Flags", xmlFlags);
174 return se;
177 public override IPermission Union (IPermission other)
179 ReflectionPermission rp = Cast (other);
180 if (other == null)
181 return Copy ();
183 if (IsUnrestricted () || rp.IsUnrestricted ())
184 return new ReflectionPermission (PermissionState.Unrestricted);
186 ReflectionPermission p = (ReflectionPermission) rp.Copy ();
187 p.Flags |= flags;
188 return p;
191 // IBuiltInPermission
192 int IBuiltInPermission.GetTokenIndex ()
194 return (int) BuiltInToken.Reflection;
197 // helpers
199 private ReflectionPermission Cast (IPermission target)
201 if (target == null)
202 return null;
204 ReflectionPermission rp = (target as ReflectionPermission);
205 if (rp == null) {
206 ThrowInvalidPermission (target, typeof (ReflectionPermission));
209 return rp;