mscorlib(4.0.0.0) API with additions
ReflectionPermission.cs
2 using System.Security.Util;
3 
5 {
8  [ComVisible(true)]
9  public sealed class ReflectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
10  {
11  internal const ReflectionPermissionFlag AllFlagsAndMore = ReflectionPermissionFlag.TypeInformation | ReflectionPermissionFlag.MemberAccess | ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.RestrictedMemberAccess;
12 
13  private ReflectionPermissionFlag m_flags;
14 
19  {
20  get
21  {
22  return m_flags;
23  }
24  set
25  {
26  VerifyAccess(value);
27  m_flags = value;
28  }
29  }
30 
35  {
36  switch (state)
37  {
38  case PermissionState.Unrestricted:
39  SetUnrestricted(unrestricted: true);
40  break;
41  case PermissionState.None:
42  SetUnrestricted(unrestricted: false);
43  break;
44  default:
45  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
46  }
47  }
48 
53  {
54  VerifyAccess(flag);
55  SetUnrestricted(unrestricted: false);
56  m_flags = flag;
57  }
58 
59  private void SetUnrestricted(bool unrestricted)
60  {
61  if (unrestricted)
62  {
63  m_flags = (ReflectionPermissionFlag.TypeInformation | ReflectionPermissionFlag.MemberAccess | ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.RestrictedMemberAccess);
64  }
65  else
66  {
67  Reset();
68  }
69  }
70 
71  private void Reset()
72  {
73  m_flags = ReflectionPermissionFlag.NoFlags;
74  }
75 
79  public bool IsUnrestricted()
80  {
81  return m_flags == (ReflectionPermissionFlag.TypeInformation | ReflectionPermissionFlag.MemberAccess | ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.RestrictedMemberAccess);
82  }
83 
88  public override IPermission Union(IPermission other)
89  {
90  if (other == null)
91  {
92  return Copy();
93  }
94  if (!VerifyType(other))
95  {
96  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
97  }
98  ReflectionPermission reflectionPermission = (ReflectionPermission)other;
99  if (IsUnrestricted() || reflectionPermission.IsUnrestricted())
100  {
101  return new ReflectionPermission(PermissionState.Unrestricted);
102  }
103  ReflectionPermissionFlag flag = m_flags | reflectionPermission.m_flags;
104  return new ReflectionPermission(flag);
105  }
106 
112  public override bool IsSubsetOf(IPermission target)
113  {
114  if (target == null)
115  {
116  return m_flags == ReflectionPermissionFlag.NoFlags;
117  }
118  try
119  {
120  ReflectionPermission reflectionPermission = (ReflectionPermission)target;
121  if (reflectionPermission.IsUnrestricted())
122  {
123  return true;
124  }
125  if (IsUnrestricted())
126  {
127  return false;
128  }
129  return (m_flags & ~reflectionPermission.m_flags) == ReflectionPermissionFlag.NoFlags;
130  }
131  catch (InvalidCastException)
132  {
133  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
134  }
135  }
136 
141  public override IPermission Intersect(IPermission target)
142  {
143  if (target == null)
144  {
145  return null;
146  }
147  if (!VerifyType(target))
148  {
149  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
150  }
151  ReflectionPermission reflectionPermission = (ReflectionPermission)target;
152  ReflectionPermissionFlag reflectionPermissionFlag = reflectionPermission.m_flags & m_flags;
153  if (reflectionPermissionFlag == ReflectionPermissionFlag.NoFlags)
154  {
155  return null;
156  }
157  return new ReflectionPermission(reflectionPermissionFlag);
158  }
159 
162  public override IPermission Copy()
163  {
164  if (IsUnrestricted())
165  {
166  return new ReflectionPermission(PermissionState.Unrestricted);
167  }
168  return new ReflectionPermission(m_flags);
169  }
170 
171  private void VerifyAccess(ReflectionPermissionFlag type)
172  {
173  if ((type & ~(ReflectionPermissionFlag.TypeInformation | ReflectionPermissionFlag.MemberAccess | ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.RestrictedMemberAccess)) != 0)
174  {
175  throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)type));
176  }
177  }
178 
181  public override SecurityElement ToXml()
182  {
183  SecurityElement securityElement = CodeAccessPermission.CreatePermissionElement(this, "System.Security.Permissions.ReflectionPermission");
184  if (!IsUnrestricted())
185  {
186  securityElement.AddAttribute("Flags", XMLUtil.BitFieldEnumToString(typeof(ReflectionPermissionFlag), m_flags));
187  }
188  else
189  {
190  securityElement.AddAttribute("Unrestricted", "true");
191  }
192  return securityElement;
193  }
194 
199  public override void FromXml(SecurityElement esd)
200  {
201  CodeAccessPermission.ValidateElement(esd, this);
202  if (XMLUtil.IsUnrestricted(esd))
203  {
204  m_flags = (ReflectionPermissionFlag.TypeInformation | ReflectionPermissionFlag.MemberAccess | ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.RestrictedMemberAccess);
205  return;
206  }
207  Reset();
208  SetUnrestricted(unrestricted: false);
209  string text = esd.Attribute("Flags");
210  if (text != null)
211  {
213  }
214  }
215 
216  int IBuiltInPermission.GetTokenIndex()
217  {
218  return GetTokenIndex();
219  }
220 
221  internal static int GetTokenIndex()
222  {
223  return 4;
224  }
225  }
226 }
Allows a permission to expose an unrestricted state.
ReflectionPermissionFlag
Specifies the permitted use of the N:System.Reflection and N:System.Reflection.Emit namespaces.
ReflectionPermission(ReflectionPermissionFlag flag)
Initializes a new instance of the T:System.Security.Permissions.ReflectionPermission class with the s...
Definition: __Canon.cs:3
The exception that is thrown for invalid casting or explicit conversion.
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...
override void FromXml(SecurityElement esd)
Reconstructs a permission with a specified state from an XML encoding.
bool IsUnrestricted()
Returns a value indicating whether the current permission is unrestricted.
ReflectionPermissionFlag Flags
Gets or sets the type of reflection allowed for the current permission.
static object Parse(Type enumType, string value)
Converts the string representation of the name or numeric value of one or more enumerated constants t...
Definition: Enum.cs:298
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Provides the base class for enumerations.
Definition: Enum.cs:14
Represents the XML object model for encoding security objects. This class cannot be inherited.
Defines the underlying structure of all code access permissions.
ReflectionPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.ReflectionPermission class with eithe...
Defines methods implemented by permission types.
Definition: IPermission.cs:7
override IPermission Copy()
Creates and returns an identical copy of the current permission.
The exception that is thrown when one of the arguments provided to a method is not valid.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
override bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
override IPermission Union(IPermission other)
Creates a permission that is the union of the current permission and the specified permission.
Controls access to non-public types and members through the N:System.Reflection APIs....
override SecurityElement ToXml()
Creates an XML encoding of the permission and its current state.