mscorlib(4.0.0.0) API with additions
ZoneIdentityPermission.cs
1 using System.Collections;
4 
6 {
9  [ComVisible(true)]
10  public sealed class ZoneIdentityPermission : CodeAccessPermission, IBuiltInPermission
11  {
12  private const uint AllZones = 31u;
13 
14  [OptionalField(VersionAdded = 2)]
15  private uint m_zones;
16 
17  [OptionalField(VersionAdded = 2)]
18  private string m_serializedPermission;
19 
20  private SecurityZone m_zone = SecurityZone.NoZone;
21 
26  {
27  get
28  {
29  SecurityZone securityZone = SecurityZone.NoZone;
30  int num = 0;
31  for (uint num2 = 1u; num2 < 31; num2 <<= 1)
32  {
33  if ((m_zones & num2) != 0)
34  {
35  if (securityZone != SecurityZone.NoZone)
36  {
37  return SecurityZone.NoZone;
38  }
39  securityZone = (SecurityZone)num;
40  }
41  num++;
42  }
43  return securityZone;
44  }
45  set
46  {
47  VerifyZone(value);
48  if (value == SecurityZone.NoZone)
49  {
50  m_zones = 0u;
51  }
52  else
53  {
54  m_zones = (uint)(1 << (int)value);
55  }
56  }
57  }
58 
59  [OnDeserialized]
60  private void OnDeserialized(StreamingContext ctx)
61  {
62  if ((ctx.State & ~(StreamingContextStates.Clone | StreamingContextStates.CrossAppDomain)) != 0)
63  {
64  if (m_serializedPermission != null)
65  {
66  FromXml(SecurityElement.FromString(m_serializedPermission));
67  m_serializedPermission = null;
68  }
69  else
70  {
71  SecurityZone = m_zone;
72  m_zone = SecurityZone.NoZone;
73  }
74  }
75  }
76 
77  [OnSerializing]
78  private void OnSerializing(StreamingContext ctx)
79  {
80  if ((ctx.State & ~(StreamingContextStates.Clone | StreamingContextStates.CrossAppDomain)) != 0)
81  {
82  m_serializedPermission = ToXml().ToString();
83  m_zone = SecurityZone;
84  }
85  }
86 
87  [OnSerialized]
88  private void OnSerialized(StreamingContext ctx)
89  {
90  if ((ctx.State & ~(StreamingContextStates.Clone | StreamingContextStates.CrossAppDomain)) != 0)
91  {
92  m_serializedPermission = null;
93  m_zone = SecurityZone.NoZone;
94  }
95  }
96 
101  {
102  switch (state)
103  {
104  case PermissionState.Unrestricted:
105  m_zones = 31u;
106  break;
107  case PermissionState.None:
108  m_zones = 0u;
109  break;
110  default:
111  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
112  }
113  }
114 
118  {
119  SecurityZone = zone;
120  }
121 
122  internal ZoneIdentityPermission(uint zones)
123  {
124  m_zones = (zones & 0x1F);
125  }
126 
127  internal void AppendZones(ArrayList zoneList)
128  {
129  int num = 0;
130  for (uint num2 = 1u; num2 < 31; num2 <<= 1)
131  {
132  if ((m_zones & num2) != 0)
133  {
134  zoneList.Add((SecurityZone)num);
135  }
136  num++;
137  }
138  }
139 
140  private static void VerifyZone(SecurityZone zone)
141  {
142  if (zone < SecurityZone.NoZone || zone > SecurityZone.Untrusted)
143  {
144  throw new ArgumentException(Environment.GetResourceString("Argument_IllegalZone"));
145  }
146  }
147 
150  public override IPermission Copy()
151  {
152  return new ZoneIdentityPermission(m_zones);
153  }
154 
160  public override bool IsSubsetOf(IPermission target)
161  {
162  if (target == null)
163  {
164  return m_zones == 0;
165  }
166  ZoneIdentityPermission zoneIdentityPermission = target as ZoneIdentityPermission;
167  if (zoneIdentityPermission == null)
168  {
169  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
170  }
171  return (m_zones & zoneIdentityPermission.m_zones) == m_zones;
172  }
173 
178  public override IPermission Intersect(IPermission target)
179  {
180  if (target == null)
181  {
182  return null;
183  }
184  ZoneIdentityPermission zoneIdentityPermission = target as ZoneIdentityPermission;
185  if (zoneIdentityPermission == null)
186  {
187  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
188  }
189  uint num = m_zones & zoneIdentityPermission.m_zones;
190  if (num == 0)
191  {
192  return null;
193  }
194  return new ZoneIdentityPermission(num);
195  }
196 
201  public override IPermission Union(IPermission target)
202  {
203  if (target == null)
204  {
205  if (m_zones == 0)
206  {
207  return null;
208  }
209  return Copy();
210  }
211  ZoneIdentityPermission zoneIdentityPermission = target as ZoneIdentityPermission;
212  if (zoneIdentityPermission == null)
213  {
214  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
215  }
216  return new ZoneIdentityPermission(m_zones | zoneIdentityPermission.m_zones);
217  }
218 
221  public override SecurityElement ToXml()
222  {
223  SecurityElement securityElement = CodeAccessPermission.CreatePermissionElement(this, "System.Security.Permissions.ZoneIdentityPermission");
224  if (SecurityZone != SecurityZone.NoZone)
225  {
226  securityElement.AddAttribute("Zone", Enum.GetName(typeof(SecurityZone), SecurityZone));
227  }
228  else
229  {
230  int num = 0;
231  for (uint num2 = 1u; num2 < 31; num2 <<= 1)
232  {
233  if ((m_zones & num2) != 0)
234  {
235  SecurityElement securityElement2 = new SecurityElement("Zone");
236  securityElement2.AddAttribute("Zone", Enum.GetName(typeof(SecurityZone), (SecurityZone)num));
237  securityElement.AddChild(securityElement2);
238  }
239  num++;
240  }
241  }
242  return securityElement;
243  }
244 
249  public override void FromXml(SecurityElement esd)
250  {
251  m_zones = 0u;
252  CodeAccessPermission.ValidateElement(esd, this);
253  string text = esd.Attribute("Zone");
254  if (text != null)
255  {
256  SecurityZone = (SecurityZone)Enum.Parse(typeof(SecurityZone), text);
257  }
258  if (esd.Children != null)
259  {
260  foreach (SecurityElement child in esd.Children)
261  {
262  text = child.Attribute("Zone");
263  int num = (int)Enum.Parse(typeof(SecurityZone), text);
264  if (num != -1)
265  {
266  m_zones |= (uint)(1 << num);
267  }
268  }
269  }
270  }
271 
272  int IBuiltInPermission.GetTokenIndex()
273  {
274  return GetTokenIndex();
275  }
276 
277  internal static int GetTokenIndex()
278  {
279  return 14;
280  }
281  }
282 }
override bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
static string GetName(Type enumType, object value)
Retrieves the name of the constant in the specified enumeration that has the specified value.
Definition: Enum.cs:482
override void FromXml(SecurityElement esd)
Reconstructs a permission with a specified state from an XML encoding.
StreamingContextStates State
Gets the source or destination of the transmitted data.
SecurityZone
Defines the integer values corresponding to security zones used by security policy.
Definition: SecurityZone.cs:8
Definition: __Canon.cs:3
Describes the source and destination of a given serialized stream, and provides an additional caller-...
override IPermission Copy()
Creates and returns an identical copy of 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
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...
void AddChild(SecurityElement child)
Adds a child element to the XML element.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
override IPermission Union(IPermission target)
Creates a permission that is the union of the current permission and the specified permission.
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.
SecurityZone SecurityZone
Gets or sets the zone represented by the current T:System.Security.Permissions.ZoneIdentityPermission...
Defines methods implemented by permission types.
Definition: IPermission.cs:7
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
ZoneIdentityPermission(SecurityZone zone)
Initializes a new instance of the T:System.Security.Permissions.ZoneIdentityPermission class to repre...
The exception that is thrown when one of the arguments provided to a method is not valid.
override SecurityElement ToXml()
Creates an XML encoding of the permission and its current state.
static SecurityElement FromString(string xml)
Creates a security element from an XML-encoded string.
Defines the identity permission for the zone from which the code originates. This class cannot be inh...
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.
ArrayList Children
Gets or sets the array of child elements of the XML element.
StreamingContextStates
Defines a set of flags that specifies the source or destination context for the stream during seriali...
ZoneIdentityPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.ZoneIdentityPermission class with the...
override string ToString()
Produces a string representation of an XML element and its constituent attributes,...
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14