2 // System.Security.Permissions.ReflectionPermission.cs
5 // Tim Coleman <tim@timcoleman.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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
{
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
;
51 flags
= ReflectionPermissionFlag
.NoFlags
;
54 public ReflectionPermission (ReflectionPermissionFlag flag
)
56 // reuse validation by the Flags property
61 public ReflectionPermissionFlag Flags
{
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");
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
;
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
);
109 if (IsUnrestricted ()) {
110 if (rp
.Flags
== ReflectionPermissionFlag
.NoFlags
)
115 if (rp
.IsUnrestricted ()) {
116 if (flags
== ReflectionPermissionFlag
.NoFlags
)
122 ReflectionPermission p
= (ReflectionPermission
) rp
.Copy ();
124 return ((p
.Flags
== ReflectionPermissionFlag
.NoFlags
) ? null : p
);
127 public override bool IsSubsetOf (IPermission target
)
129 ReflectionPermission rp
= Cast (target
);
131 return (flags
== ReflectionPermissionFlag
.NoFlags
);
133 if (IsUnrestricted ())
134 return rp
.IsUnrestricted ();
135 else if (rp
.IsUnrestricted ())
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");
153 if (flags
== ReflectionPermissionFlag
.NoFlags
)
154 se
.AddAttribute ("Flags", "NoFlags");
155 else if ((flags
& ReflectionPermissionFlag
.AllFlags
) == ReflectionPermissionFlag
.AllFlags
)
156 se
.AddAttribute ("Flags", "AllFlags");
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)
164 xmlFlags
+= "ReflectionEmit";
166 if ((flags
& ReflectionPermissionFlag
.TypeInformation
) == ReflectionPermissionFlag
.TypeInformation
) {
167 if (xmlFlags
.Length
> 0)
169 xmlFlags
+= "TypeInformation";
171 se
.AddAttribute ("Flags", xmlFlags
);
177 public override IPermission
Union (IPermission other
)
179 ReflectionPermission rp
= Cast (other
);
183 if (IsUnrestricted () || rp
.IsUnrestricted ())
184 return new ReflectionPermission (PermissionState
.Unrestricted
);
186 ReflectionPermission p
= (ReflectionPermission
) rp
.Copy ();
191 // IBuiltInPermission
192 int IBuiltInPermission
.GetTokenIndex ()
194 return (int) BuiltInToken
.Reflection
;
199 private ReflectionPermission
Cast (IPermission target
)
204 ReflectionPermission rp
= (target
as ReflectionPermission
);
206 ThrowInvalidPermission (target
, typeof (ReflectionPermission
));