2 // System.Security.Permissions.ReflectionPermission.cs
5 // Tim Coleman <tim@timcoleman.com>
6 // Sebastien Pouliot (spouliot@motus.com)
8 // Copyright (C) 2002, Tim Coleman
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 namespace System.Security.Permissions {
40 public sealed class ReflectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
44 ReflectionPermissionFlag flags;
45 PermissionState state;
51 public ReflectionPermission (PermissionState state)
54 case PermissionState.None:
55 flags = ReflectionPermissionFlag.NoFlags;
57 case PermissionState.Unrestricted:
58 flags = ReflectionPermissionFlag.AllFlags;
61 throw new ArgumentException ("Invalid PermissionState");
65 public ReflectionPermission (ReflectionPermissionFlag flag)
70 #endregion // Constructors
74 public ReflectionPermissionFlag Flags {
76 set { flags = value; }
79 #endregion // Properties
83 public override IPermission Copy ()
85 return new ReflectionPermission (flags);
88 public override void FromXml (SecurityElement esd)
91 throw new ArgumentNullException ("esd");
92 if (esd.Tag != "IPermission")
93 throw new ArgumentException ("not IPermission");
94 if (!(esd.Attributes ["class"] as string).StartsWith ("System.Security.Permissions.ReflectionPermission"))
95 throw new ArgumentException ("not ReflectionPermission");
96 if ((esd.Attributes ["version"] as string) != "1")
97 throw new ArgumentException ("wrong version");
99 if ((esd.Attributes ["Unrestricted"] as string) == "true")
100 flags = ReflectionPermissionFlag.AllFlags;
102 flags = ReflectionPermissionFlag.NoFlags;
103 string xmlFlags = (esd.Attributes ["Flags"] as string);
104 if (xmlFlags.IndexOf ("MemberAccess") >= 0)
105 flags |= ReflectionPermissionFlag.MemberAccess;
106 if (xmlFlags.IndexOf ("ReflectionEmit") >= 0)
107 flags |= ReflectionPermissionFlag.ReflectionEmit;
108 if (xmlFlags.IndexOf ("TypeInformation") >= 0)
109 flags |= ReflectionPermissionFlag.TypeInformation;
113 public override IPermission Intersect (IPermission target)
117 if (! (target is ReflectionPermission))
118 throw new ArgumentException ("wrong type");
120 ReflectionPermission o = (ReflectionPermission) target;
121 if (IsUnrestricted ()) {
122 if (o.Flags == ReflectionPermissionFlag.NoFlags)
127 if (o.IsUnrestricted ()) {
128 if (flags == ReflectionPermissionFlag.NoFlags)
134 ReflectionPermission p = (ReflectionPermission) o.Copy ();
136 return ((p.Flags == ReflectionPermissionFlag.NoFlags) ? null : p);
139 public override bool IsSubsetOf (IPermission target)
142 return (flags == ReflectionPermissionFlag.NoFlags);
144 if (! (target is ReflectionPermission))
145 throw new ArgumentException ("wrong type");
147 ReflectionPermission o = (ReflectionPermission) target;
148 if (IsUnrestricted ())
149 return o.IsUnrestricted ();
150 else if (o.IsUnrestricted ())
153 return ((flags & o.Flags) == flags);
156 public bool IsUnrestricted ()
158 return (flags == ReflectionPermissionFlag.AllFlags);
161 public override SecurityElement ToXml ()
163 SecurityElement se = Element (this, 1);
164 if (IsUnrestricted ()) {
165 se.AddAttribute ("Unrestricted", "true");
168 if (flags == ReflectionPermissionFlag.NoFlags)
169 se.AddAttribute ("Flags", "NoFlags");
170 else if ((flags & ReflectionPermissionFlag.AllFlags) == ReflectionPermissionFlag.AllFlags)
171 se.AddAttribute ("Flags", "AllFlags");
173 string xmlFlags = "";
174 if ((flags & ReflectionPermissionFlag.MemberAccess) == ReflectionPermissionFlag.MemberAccess)
175 xmlFlags = "MemberAccess";
176 if ((flags & ReflectionPermissionFlag.ReflectionEmit) == ReflectionPermissionFlag.ReflectionEmit) {
177 if (xmlFlags.Length > 0)
179 xmlFlags += "ReflectionEmit";
181 if ((flags & ReflectionPermissionFlag.TypeInformation) == ReflectionPermissionFlag.TypeInformation) {
182 if (xmlFlags.Length > 0)
184 xmlFlags += "TypeInformation";
186 se.AddAttribute ("Flags", xmlFlags);
192 public override IPermission Union (IPermission other)
196 if (! (other is ReflectionPermission))
197 throw new ArgumentException ("wrong type");
199 ReflectionPermission o = (ReflectionPermission) other;
200 if (IsUnrestricted () || o.IsUnrestricted ())
201 return new ReflectionPermission (PermissionState.Unrestricted);
203 ReflectionPermission p = (ReflectionPermission) o.Copy ();
208 // IBuiltInPermission
209 int IBuiltInPermission.GetTokenIndex ()
214 #endregion // Methods