mscorlib(4.0.0.0) API with additions
CodeGroup.cs
1 using System.Collections;
4 using System.Security.Util;
5 
7 {
10  [ComVisible(true)]
11  public abstract class CodeGroup
12  {
13  private IMembershipCondition m_membershipCondition;
14 
15  private IList m_children;
16 
17  private PolicyStatement m_policy;
18 
19  private SecurityElement m_element;
20 
21  private PolicyLevel m_parentLevel;
22 
23  private string m_name;
24 
25  private string m_description;
26 
31  public IList Children
32  {
33  [SecuritySafeCritical]
34  get
35  {
36  if (m_children == null)
37  {
38  ParseChildren();
39  }
40  lock (this)
41  {
42  IList list = new ArrayList(m_children.Count);
43  IEnumerator enumerator = m_children.GetEnumerator();
44  while (enumerator.MoveNext())
45  {
46  list.Add(((CodeGroup)enumerator.Current).Copy());
47  }
48  return list;
49  }
50  }
51  set
52  {
53  if (value == null)
54  {
55  throw new ArgumentNullException("Children");
56  }
57  ArrayList arrayList = ArrayList.Synchronized(new ArrayList(value.Count));
58  IEnumerator enumerator = value.GetEnumerator();
59  while (enumerator.MoveNext())
60  {
61  CodeGroup codeGroup = enumerator.Current as CodeGroup;
62  if (codeGroup == null)
63  {
64  throw new ArgumentException(Environment.GetResourceString("Argument_CodeGroupChildrenMustBeCodeGroups"));
65  }
66  arrayList.Add(codeGroup.Copy());
67  }
68  m_children = arrayList;
69  }
70  }
71 
76  {
77  [SecuritySafeCritical]
78  get
79  {
80  if (m_membershipCondition == null && m_element != null)
81  {
82  ParseMembershipCondition();
83  }
84  return m_membershipCondition.Copy();
85  }
86  set
87  {
88  if (value == null)
89  {
90  throw new ArgumentNullException("MembershipCondition");
91  }
92  m_membershipCondition = value.Copy();
93  }
94  }
95 
99  {
100  get
101  {
102  if (m_policy == null && m_element != null)
103  {
104  ParsePolicy();
105  }
106  if (m_policy != null)
107  {
108  return m_policy.Copy();
109  }
110  return null;
111  }
112  set
113  {
114  if (value != null)
115  {
116  m_policy = value.Copy();
117  }
118  else
119  {
120  m_policy = null;
121  }
122  }
123  }
124 
127  public string Name
128  {
129  get
130  {
131  return m_name;
132  }
133  set
134  {
135  m_name = value;
136  }
137  }
138 
141  public string Description
142  {
143  get
144  {
145  return m_description;
146  }
147  set
148  {
149  m_description = value;
150  }
151  }
152 
155  public virtual string PermissionSetName
156  {
157  get
158  {
159  if (m_policy == null && m_element != null)
160  {
161  ParsePolicy();
162  }
163  if (m_policy == null)
164  {
165  return null;
166  }
167  return (m_policy.GetPermissionSetNoCopy() as NamedPermissionSet)?.Name;
168  }
169  }
170 
173  public virtual string AttributeString
174  {
175  get
176  {
177  if (m_policy == null && m_element != null)
178  {
179  ParsePolicy();
180  }
181  if (m_policy != null)
182  {
183  return m_policy.AttributeString;
184  }
185  return null;
186  }
187  }
188 
191  public abstract string MergeLogic
192  {
193  get;
194  }
195 
196  internal CodeGroup()
197  {
198  }
199 
200  internal CodeGroup(IMembershipCondition membershipCondition, PermissionSet permSet)
201  {
202  m_membershipCondition = membershipCondition;
203  m_policy = new PolicyStatement();
204  m_policy.SetPermissionSetNoCopy(permSet);
205  m_children = ArrayList.Synchronized(new ArrayList());
206  m_element = null;
207  m_parentLevel = null;
208  }
209 
215  protected CodeGroup(IMembershipCondition membershipCondition, PolicyStatement policy)
216  {
217  if (membershipCondition == null)
218  {
219  throw new ArgumentNullException("membershipCondition");
220  }
221  if (policy == null)
222  {
223  m_policy = null;
224  }
225  else
226  {
227  m_policy = policy.Copy();
228  }
229  m_membershipCondition = membershipCondition.Copy();
230  m_children = ArrayList.Synchronized(new ArrayList());
231  m_element = null;
232  m_parentLevel = null;
233  }
234 
239  [SecuritySafeCritical]
240  public void AddChild(CodeGroup group)
241  {
242  if (group == null)
243  {
244  throw new ArgumentNullException("group");
245  }
246  if (m_children == null)
247  {
248  ParseChildren();
249  }
250  lock (this)
251  {
252  m_children.Add(group.Copy());
253  }
254  }
255 
256  [SecurityCritical]
257  internal void AddChildInternal(CodeGroup group)
258  {
259  if (group == null)
260  {
261  throw new ArgumentNullException("group");
262  }
263  if (m_children == null)
264  {
265  ParseChildren();
266  }
267  lock (this)
268  {
269  m_children.Add(group);
270  }
271  }
272 
276  [SecuritySafeCritical]
277  public void RemoveChild(CodeGroup group)
278  {
279  if (group != null)
280  {
281  if (m_children == null)
282  {
283  ParseChildren();
284  }
285  lock (this)
286  {
287  int num = m_children.IndexOf(group);
288  if (num != -1)
289  {
290  m_children.RemoveAt(num);
291  }
292  }
293  }
294  }
295 
296  [SecurityCritical]
297  internal IList GetChildrenInternal()
298  {
299  if (m_children == null)
300  {
301  ParseChildren();
302  }
303  return m_children;
304  }
305 
309  public abstract PolicyStatement Resolve(Evidence evidence);
310 
314  public abstract CodeGroup ResolveMatchingCodeGroups(Evidence evidence);
315 
318  public abstract CodeGroup Copy();
319 
323  {
324  return ToXml(null);
325  }
326 
330  public void FromXml(SecurityElement e)
331  {
332  FromXml(e, null);
333  }
334 
338  [SecuritySafeCritical]
340  {
341  return ToXml(level, GetTypeName());
342  }
343 
344  internal virtual string GetTypeName()
345  {
346  return GetType().FullName;
347  }
348 
349  [SecurityCritical]
350  internal SecurityElement ToXml(PolicyLevel level, string policyClassName)
351  {
352  if (m_membershipCondition == null && m_element != null)
353  {
354  ParseMembershipCondition();
355  }
356  if (m_children == null)
357  {
358  ParseChildren();
359  }
360  if (m_policy == null && m_element != null)
361  {
362  ParsePolicy();
363  }
364  SecurityElement securityElement = new SecurityElement("CodeGroup");
365  XMLUtil.AddClassAttribute(securityElement, GetType(), policyClassName);
366  securityElement.AddAttribute("version", "1");
367  securityElement.AddChild(m_membershipCondition.ToXml(level));
368  if (m_policy != null)
369  {
370  PermissionSet permissionSetNoCopy = m_policy.GetPermissionSetNoCopy();
371  NamedPermissionSet namedPermissionSet = permissionSetNoCopy as NamedPermissionSet;
372  if (namedPermissionSet != null && level != null && level.GetNamedPermissionSetInternal(namedPermissionSet.Name) != null)
373  {
374  securityElement.AddAttribute("PermissionSetName", namedPermissionSet.Name);
375  }
376  else if (!permissionSetNoCopy.IsEmpty())
377  {
378  securityElement.AddChild(permissionSetNoCopy.ToXml());
379  }
380  if (m_policy.Attributes != 0)
381  {
382  securityElement.AddAttribute("Attributes", XMLUtil.BitFieldEnumToString(typeof(PolicyStatementAttribute), m_policy.Attributes));
383  }
384  }
385  if (m_children.Count > 0)
386  {
387  lock (this)
388  {
389  IEnumerator enumerator = m_children.GetEnumerator();
390  while (enumerator.MoveNext())
391  {
392  securityElement.AddChild(((CodeGroup)enumerator.Current).ToXml(level));
393  }
394  }
395  }
396  if (m_name != null)
397  {
398  securityElement.AddAttribute("Name", SecurityElement.Escape(m_name));
399  }
400  if (m_description != null)
401  {
402  securityElement.AddAttribute("Description", SecurityElement.Escape(m_description));
403  }
404  CreateXml(securityElement, level);
405  return securityElement;
406  }
407 
411  protected virtual void CreateXml(SecurityElement element, PolicyLevel level)
412  {
413  }
414 
419  public void FromXml(SecurityElement e, PolicyLevel level)
420  {
421  if (e == null)
422  {
423  throw new ArgumentNullException("e");
424  }
425  lock (this)
426  {
427  m_element = e;
428  m_parentLevel = level;
429  m_children = null;
430  m_membershipCondition = null;
431  m_policy = null;
432  m_name = e.Attribute("Name");
433  m_description = e.Attribute("Description");
434  ParseXml(e, level);
435  }
436  }
437 
441  protected virtual void ParseXml(SecurityElement e, PolicyLevel level)
442  {
443  }
444 
445  [SecurityCritical]
446  private bool ParseMembershipCondition(bool safeLoad)
447  {
448  lock (this)
449  {
450  IMembershipCondition membershipCondition = null;
451  SecurityElement securityElement = m_element.SearchForChildByTag("IMembershipCondition");
452  if (securityElement == null)
453  {
454  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidXMLElement"), "IMembershipCondition", GetType().FullName));
455  }
456  try
457  {
458  membershipCondition = XMLUtil.CreateMembershipCondition(securityElement);
459  if (membershipCondition == null)
460  {
461  return false;
462  }
463  }
464  catch (Exception innerException)
465  {
466  throw new ArgumentException(Environment.GetResourceString("Argument_MembershipConditionElement"), innerException);
467  }
468  membershipCondition.FromXml(securityElement, m_parentLevel);
469  m_membershipCondition = membershipCondition;
470  return true;
471  }
472  }
473 
474  [SecurityCritical]
475  private void ParseMembershipCondition()
476  {
477  ParseMembershipCondition(safeLoad: false);
478  }
479 
480  [SecurityCritical]
481  internal void ParseChildren()
482  {
483  lock (this)
484  {
485  ArrayList arrayList = ArrayList.Synchronized(new ArrayList());
486  if (m_element != null && m_element.InternalChildren != null)
487  {
488  m_element.Children = (ArrayList)m_element.InternalChildren.Clone();
489  ArrayList arrayList2 = ArrayList.Synchronized(new ArrayList());
490  Evidence evidence = new Evidence();
491  int count = m_element.InternalChildren.Count;
492  int num = 0;
493  while (num < count)
494  {
495  SecurityElement securityElement = (SecurityElement)m_element.Children[num];
496  if (securityElement.Tag.Equals("CodeGroup"))
497  {
498  CodeGroup codeGroup = XMLUtil.CreateCodeGroup(securityElement);
499  if (codeGroup != null)
500  {
501  codeGroup.FromXml(securityElement, m_parentLevel);
502  if (ParseMembershipCondition(safeLoad: true))
503  {
504  codeGroup.Resolve(evidence);
505  codeGroup.MembershipCondition.Check(evidence);
506  arrayList.Add(codeGroup);
507  num++;
508  }
509  else
510  {
511  m_element.InternalChildren.RemoveAt(num);
512  count = m_element.InternalChildren.Count;
513  arrayList2.Add(new CodeGroupPositionMarker(num, arrayList.Count, securityElement));
514  }
515  }
516  else
517  {
518  m_element.InternalChildren.RemoveAt(num);
519  count = m_element.InternalChildren.Count;
520  arrayList2.Add(new CodeGroupPositionMarker(num, arrayList.Count, securityElement));
521  }
522  }
523  else
524  {
525  num++;
526  }
527  }
528  IEnumerator enumerator = arrayList2.GetEnumerator();
529  while (enumerator.MoveNext())
530  {
531  CodeGroupPositionMarker codeGroupPositionMarker = (CodeGroupPositionMarker)enumerator.Current;
532  CodeGroup codeGroup2 = XMLUtil.CreateCodeGroup(codeGroupPositionMarker.element);
533  if (codeGroup2 == null)
534  {
535  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_FailedCodeGroup"), codeGroupPositionMarker.element.Attribute("class")));
536  }
537  codeGroup2.FromXml(codeGroupPositionMarker.element, m_parentLevel);
538  codeGroup2.Resolve(evidence);
539  codeGroup2.MembershipCondition.Check(evidence);
540  arrayList.Insert(codeGroupPositionMarker.groupIndex, codeGroup2);
541  m_element.InternalChildren.Insert(codeGroupPositionMarker.elementIndex, codeGroupPositionMarker.element);
542  }
543  }
544  m_children = arrayList;
545  }
546  }
547 
548  private void ParsePolicy()
549  {
550  while (true)
551  {
552  PolicyStatement policyStatement = new PolicyStatement();
553  bool flag = false;
554  SecurityElement securityElement = new SecurityElement("PolicyStatement");
555  securityElement.AddAttribute("version", "1");
556  SecurityElement element = m_element;
557  lock (this)
558  {
559  if (m_element != null)
560  {
561  string text = m_element.Attribute("PermissionSetName");
562  if (text != null)
563  {
564  securityElement.AddAttribute("PermissionSetName", text);
565  flag = true;
566  }
567  else
568  {
569  SecurityElement securityElement2 = m_element.SearchForChildByTag("PermissionSet");
570  if (securityElement2 != null)
571  {
572  securityElement.AddChild(securityElement2);
573  flag = true;
574  }
575  else
576  {
577  securityElement.AddChild(new PermissionSet(fUnrestricted: false).ToXml());
578  flag = true;
579  }
580  }
581  string text2 = m_element.Attribute("Attributes");
582  if (text2 != null)
583  {
584  securityElement.AddAttribute("Attributes", text2);
585  flag = true;
586  }
587  }
588  }
589  if (flag)
590  {
591  policyStatement.FromXml(securityElement, m_parentLevel);
592  }
593  else
594  {
595  policyStatement.PermissionSet = null;
596  }
597  lock (this)
598  {
599  if (element == m_element && m_policy == null)
600  {
601  m_policy = policyStatement;
602  }
603  else if (m_policy == null)
604  {
605  continue;
606  }
607  }
608  break;
609  }
610  if (m_policy != null && m_children != null)
611  {
612  IMembershipCondition membershipCondition = m_membershipCondition;
613  }
614  }
615 
620  [SecuritySafeCritical]
621  public override bool Equals(object o)
622  {
623  CodeGroup codeGroup = o as CodeGroup;
624  if (codeGroup != null && GetType().Equals(codeGroup.GetType()) && object.Equals(m_name, codeGroup.m_name) && object.Equals(m_description, codeGroup.m_description))
625  {
626  if (m_membershipCondition == null && m_element != null)
627  {
628  ParseMembershipCondition();
629  }
630  if (codeGroup.m_membershipCondition == null && codeGroup.m_element != null)
631  {
632  codeGroup.ParseMembershipCondition();
633  }
634  if (object.Equals(m_membershipCondition, codeGroup.m_membershipCondition))
635  {
636  return true;
637  }
638  }
639  return false;
640  }
641 
648  [SecuritySafeCritical]
649  public bool Equals(CodeGroup cg, bool compareChildren)
650  {
651  if (!Equals(cg))
652  {
653  return false;
654  }
655  if (compareChildren)
656  {
657  if (m_children == null)
658  {
659  ParseChildren();
660  }
661  if (cg.m_children == null)
662  {
663  cg.ParseChildren();
664  }
665  ArrayList arrayList = new ArrayList(m_children);
666  ArrayList arrayList2 = new ArrayList(cg.m_children);
667  if (arrayList.Count != arrayList2.Count)
668  {
669  return false;
670  }
671  for (int i = 0; i < arrayList.Count; i++)
672  {
673  if (!((CodeGroup)arrayList[i]).Equals((CodeGroup)arrayList2[i], compareChildren: true))
674  {
675  return false;
676  }
677  }
678  }
679  return true;
680  }
681 
684  [SecuritySafeCritical]
685  public override int GetHashCode()
686  {
687  if (m_membershipCondition == null && m_element != null)
688  {
689  ParseMembershipCondition();
690  }
691  if (m_name != null || m_membershipCondition != null)
692  {
693  return ((m_name != null) ? m_name.GetHashCode() : 0) + ((m_membershipCondition != null) ? m_membershipCondition.GetHashCode() : 0);
694  }
695  return GetType().GetHashCode();
696  }
697  }
698 }
abstract CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
When overridden in a derived class, resolves matching code groups.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
bool MoveNext()
Advances the enumerator to the next element of the collection.
bool Equals(CodeGroup cg, bool compareChildren)
Determines whether the specified code group is equivalent to the current code group,...
Definition: CodeGroup.cs:649
IMembershipCondition Copy()
Creates an equivalent copy of the membership condition.
virtual void Insert(int index, object value)
Inserts an element into the T:System.Collections.ArrayList at the specified index.
Definition: ArrayList.cs:2698
virtual void ParseXml(SecurityElement e, PolicyLevel level)
When overridden in a derived class, reconstructs properties and internal state specific to a derived ...
Definition: CodeGroup.cs:441
PolicyStatement Copy()
Creates an equivalent copy of the current policy statement.
virtual void RemoveAt(int index)
Removes the element at the specified index of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2847
Represents a non-generic collection of objects that can be individually accessed by index.
Definition: IList.cs:8
static IList Synchronized(IList list)
Returns an T:System.Collections.IList wrapper that is synchronized (thread safe).
Definition: ArrayList.cs:3057
Represents the security policy levels for the common language runtime. This class cannot be inherited...
Definition: PolicyLevel.cs:15
void RemoveAt(int index)
Removes the T:System.Collections.IList item at the specified index.
abstract CodeGroup Copy()
When overridden in a derived class, makes a deep copy of the current code group.
void FromXml(SecurityElement e)
Reconstructs a security object with a given state from an XML encoding.
Definition: CodeGroup.cs:330
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
PolicyStatementAttribute
Defines special attribute flags for security policy on code groups.
string Name
Gets or sets the name of the code group.
Definition: CodeGroup.cs:128
IMembershipCondition MembershipCondition
Gets or sets the code group's membership condition.
Definition: CodeGroup.cs:76
SecurityElement SearchForChildByTag(string tag)
Finds a child by its tag name.
virtual void CreateXml(SecurityElement element, PolicyLevel level)
When overridden in a derived class, serializes properties and internal state specific to a derived co...
Definition: CodeGroup.cs:411
int IndexOf(object value)
Determines the index of a specific item in the T:System.Collections.IList.
virtual object Clone()
Creates a shallow copy of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2473
void FromXml(SecurityElement e)
Reconstructs a security object with a specified state from an XML encoding.
Represents the statement of a T:System.Security.Policy.CodeGroup describing the permissions and other...
void AddChild(CodeGroup group)
Adds a child code group to the current code group.
Definition: CodeGroup.cs:240
CodeGroup(IMembershipCondition membershipCondition, PolicyStatement policy)
Initializes a new instance of T:System.Security.Policy.CodeGroup.
Definition: CodeGroup.cs:215
Defines a permission set that has a name and description associated with it. This class cannot be inh...
abstract string MergeLogic
When overridden in a derived class, gets the merge logic for the code group.
Definition: CodeGroup.cs:192
void AddChild(SecurityElement child)
Adds a child element to the XML element.
SecurityElement ToXml()
Creates an XML encoding of the security object and its current state.
Definition: CodeGroup.cs:322
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
IList Children
Gets or sets an ordered list of the child code groups of a code group.
Definition: CodeGroup.cs:32
Represents a collection that can contain many different types of permissions.
Defines the test to determine whether a code assembly is a member of a code group.
Represents the XML object model for encoding security objects. This class cannot be inherited.
string Description
Gets or sets the description of the code group.
Definition: CodeGroup.cs:142
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
Represents the abstract base class from which all implementations of code groups must derive.
Definition: CodeGroup.cs:11
string AttributeString
Gets a string representation of the attributes of the policy statement.
PolicyStatement PolicyStatement
Gets or sets the policy statement associated with the code group.
Definition: CodeGroup.cs:99
int Add(object value)
Adds an item to the T:System.Collections.IList.
SecurityElement ToXml()
Creates an XML encoding of the security object and its current state.
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
void RemoveChild(CodeGroup group)
Removes the specified child code group.
Definition: CodeGroup.cs:277
virtual string? PermissionSetName
Gets the name of the named permission set for the code group.
Definition: CodeGroup.cs:156
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual string AttributeString
Gets a string representation of the attributes of the policy statement for the code group.
Definition: CodeGroup.cs:174
Specifies that the class can be serialized.
ArrayList Children
Gets or sets the array of child elements of the XML element.
virtual IEnumerator GetEnumerator()
Returns an enumerator for the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:2615
override int GetHashCode()
Gets the hash code of the current code group.
Definition: CodeGroup.cs:685
abstract PolicyStatement Resolve(Evidence evidence)
When overridden in a derived class, resolves policy for the code group and its descendants for a set ...
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
SecurityElement ToXml(PolicyLevel level)
Creates an XML encoding of the security object, its current state, and the policy level within which ...
Definition: CodeGroup.cs:339
override bool Equals(object o)
Determines whether the specified code group is equivalent to the current code group.
Definition: CodeGroup.cs:621
PolicyStatementAttribute Attributes
Gets or sets the attributes of the policy statement.
void FromXml(SecurityElement e, PolicyLevel level)
Reconstructs a security object with a given state and policy level from an XML encoding.
Definition: CodeGroup.cs:419
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14