mscorlib(4.0.0.0) API with additions
IdentityReferenceCollection.cs
1 using System.Collections;
5 
7 {
9  [ComVisible(false)]
10  public class IdentityReferenceCollection : ICollection<IdentityReference>, IEnumerable<IdentityReference>, IEnumerable
11  {
12  private List<IdentityReference> _Identities;
13 
16  public int Count => _Identities.Count;
17 
20  public bool IsReadOnly => false;
21 
25  public IdentityReference this[int index]
26  {
27  get
28  {
29  return _Identities[index];
30  }
31  set
32  {
33  if (value == null)
34  {
35  throw new ArgumentNullException("value");
36  }
37  _Identities[index] = value;
38  }
39  }
40 
41  internal List<IdentityReference> Identities => _Identities;
42 
45  : this(0)
46  {
47  }
48 
51  public IdentityReferenceCollection(int capacity)
52  {
53  _Identities = new List<IdentityReference>(capacity);
54  }
55 
59  public void CopyTo(IdentityReference[] array, int offset)
60  {
61  _Identities.CopyTo(0, array, offset, Count);
62  }
63 
68  public void Add(IdentityReference identity)
69  {
70  if (identity == null)
71  {
72  throw new ArgumentNullException("identity");
73  }
74  _Identities.Add(identity);
75  }
76 
83  public bool Remove(IdentityReference identity)
84  {
85  if (identity == null)
86  {
87  throw new ArgumentNullException("identity");
88  }
89  if (Contains(identity))
90  {
91  _Identities.Remove(identity);
92  return true;
93  }
94  return false;
95  }
96 
98  public void Clear()
99  {
100  _Identities.Clear();
101  }
102 
109  public bool Contains(IdentityReference identity)
110  {
111  if (identity == null)
112  {
113  throw new ArgumentNullException("identity");
114  }
115  return _Identities.Contains(identity);
116  }
117 
121  {
122  return GetEnumerator();
123  }
124 
128  {
129  return new IdentityReferenceEnumerator(this);
130  }
131 
136  {
137  return Translate(targetType, forceSuccess: false);
138  }
139 
144  [SecuritySafeCritical]
145  [SecurityPermission(SecurityAction.Demand, ControlPrincipal = true)]
146  public IdentityReferenceCollection Translate(Type targetType, bool forceSuccess)
147  {
148  if (targetType == null)
149  {
150  throw new ArgumentNullException("targetType");
151  }
152  if (!targetType.IsSubclassOf(typeof(IdentityReference)))
153  {
154  throw new ArgumentException(Environment.GetResourceString("IdentityReference_MustBeIdentityReference"), "targetType");
155  }
156  if (Identities.Count == 0)
157  {
158  return new IdentityReferenceCollection();
159  }
160  int num = 0;
161  int num2 = 0;
162  for (int i = 0; i < Identities.Count; i++)
163  {
164  Type type = Identities[i].GetType();
165  if (type == targetType)
166  {
167  continue;
168  }
169  if (type == typeof(SecurityIdentifier))
170  {
171  num++;
172  continue;
173  }
174  if (type == typeof(NTAccount))
175  {
176  num2++;
177  continue;
178  }
179  throw new SystemException();
180  }
181  bool flag = false;
182  IdentityReferenceCollection identityReferenceCollection = null;
183  IdentityReferenceCollection identityReferenceCollection2 = null;
184  if (num == Count)
185  {
186  flag = true;
187  identityReferenceCollection = this;
188  }
189  else if (num > 0)
190  {
191  identityReferenceCollection = new IdentityReferenceCollection(num);
192  }
193  if (num2 == Count)
194  {
195  flag = true;
196  identityReferenceCollection2 = this;
197  }
198  else if (num2 > 0)
199  {
200  identityReferenceCollection2 = new IdentityReferenceCollection(num2);
201  }
202  IdentityReferenceCollection identityReferenceCollection3 = null;
203  if (!flag)
204  {
205  identityReferenceCollection3 = new IdentityReferenceCollection(Identities.Count);
206  for (int j = 0; j < Identities.Count; j++)
207  {
208  IdentityReference identityReference = this[j];
209  Type type2 = identityReference.GetType();
210  if (type2 == targetType)
211  {
212  continue;
213  }
214  if (type2 == typeof(SecurityIdentifier))
215  {
216  identityReferenceCollection.Add(identityReference);
217  continue;
218  }
219  if (type2 == typeof(NTAccount))
220  {
221  identityReferenceCollection2.Add(identityReference);
222  continue;
223  }
224  throw new SystemException();
225  }
226  }
227  bool someFailed = false;
228  IdentityReferenceCollection identityReferenceCollection4 = null;
229  IdentityReferenceCollection identityReferenceCollection5 = null;
230  if (num > 0)
231  {
232  identityReferenceCollection4 = SecurityIdentifier.Translate(identityReferenceCollection, targetType, out someFailed);
233  if (flag && (!forceSuccess || !someFailed))
234  {
235  identityReferenceCollection3 = identityReferenceCollection4;
236  }
237  }
238  if (num2 > 0)
239  {
240  identityReferenceCollection5 = NTAccount.Translate(identityReferenceCollection2, targetType, out someFailed);
241  if (flag && (!forceSuccess || !someFailed))
242  {
243  identityReferenceCollection3 = identityReferenceCollection5;
244  }
245  }
246  if (forceSuccess && someFailed)
247  {
248  identityReferenceCollection3 = new IdentityReferenceCollection();
249  if (identityReferenceCollection4 != null)
250  {
251  foreach (IdentityReference item in identityReferenceCollection4)
252  {
253  if (item.GetType() != targetType)
254  {
255  identityReferenceCollection3.Add(item);
256  }
257  }
258  }
259  if (identityReferenceCollection5 != null)
260  {
261  foreach (IdentityReference item2 in identityReferenceCollection5)
262  {
263  if (item2.GetType() != targetType)
264  {
265  identityReferenceCollection3.Add(item2);
266  }
267  }
268  }
269  throw new IdentityNotMappedException(Environment.GetResourceString("IdentityReference_IdentityNotMapped"), identityReferenceCollection3);
270  }
271  if (!flag)
272  {
273  num = 0;
274  num2 = 0;
275  identityReferenceCollection3 = new IdentityReferenceCollection(Identities.Count);
276  for (int k = 0; k < Identities.Count; k++)
277  {
278  IdentityReference identityReference2 = this[k];
279  Type type3 = identityReference2.GetType();
280  if (type3 == targetType)
281  {
282  identityReferenceCollection3.Add(identityReference2);
283  continue;
284  }
285  if (type3 == typeof(SecurityIdentifier))
286  {
287  identityReferenceCollection3.Add(identityReferenceCollection4[num++]);
288  continue;
289  }
290  if (type3 == typeof(NTAccount))
291  {
292  identityReferenceCollection3.Add(identityReferenceCollection5[num2++]);
293  continue;
294  }
295  throw new SystemException();
296  }
297  }
298  return identityReferenceCollection3;
299  }
300  }
301 }
bool Remove(IdentityReference identity)
Removes the specified T:System.Security.Principal.IdentityReference object from the collection.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
Definition: List.cs:296
Represents an identity and is the base class for the T:System.Security.Principal.NTAccount and T:Syst...
Serves as the base class for system exceptions namespace.
bool IsReadOnly
Gets a value that indicates whether the T:System.Security.Principal.IdentityReferenceCollection colle...
void Add(IdentityReference identity)
Adds an T:System.Security.Principal.IdentityReference object to the T:System.Security....
Represents an exception for a principal whose identity could not be mapped to a known identity.
Definition: __Canon.cs:3
int Count
Gets the number of items in the T:System.Security.Principal.IdentityReferenceCollection collection.
IdentityReferenceCollection(int capacity)
Initializes a new instance of the T:System.Security.Principal.IdentityReferenceCollection class by us...
void Clear()
Clears all T:System.Security.Principal.IdentityReference objects from the T:System....
Represents a user or group account.
Definition: NTAccount.cs:10
virtual bool IsSubclassOf(Type c)
Determines whether the current T:System.Type derives from the specified T:System.Type.
Definition: Type.cs:2664
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
Definition: List.cs:510
SecurityAction
Specifies the security actions that can be performed using declarative security.
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
IdentityReferenceCollection Translate(Type targetType, bool forceSuccess)
Converts the objects in the collection to the specified type and uses the specified fault tolerance t...
bool Contains(T item)
Determines whether an element is in the T:System.Collections.Generic.List`1.
Definition: List.cs:629
Represents a collection of T:System.Security.Principal.IdentityReference objects and provides a means...
IdentityReferenceCollection Translate(Type targetType)
Converts the objects in the collection to the specified type. Calling this method is the same as call...
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
The exception that is thrown when one of the arguments provided to a method is not valid.
void Clear()
Removes all elements from the T:System.Collections.Generic.List`1.
Definition: List.cs:614
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
IEnumerator< IdentityReference > GetEnumerator()
Gets an enumerator that can be used to iterate through the T:System.Security.Principal....
Represents a security identifier (SID) and provides marshaling and comparison operations for SIDs.
bool Contains(IdentityReference identity)
Indicates whether the T:System.Security.Principal.IdentityReferenceCollection collection contains the...
override IdentityReference Translate(Type targetType)
Translates the account name represented by the T:System.Security.Principal.NTAccount object into anot...
Definition: NTAccount.cs:111
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
IdentityReferenceCollection()
Initializes a new instance of the T:System.Security.Principal.IdentityReferenceCollection class with ...
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
void CopyTo(T[] array)
Copies the entire T:System.Collections.Generic.List`1 to a compatible one-dimensional array,...
Definition: List.cs:694
void CopyTo(IdentityReference[] array, int offset)
Copies the T:System.Security.Principal.IdentityReferenceCollection collection to an T:System....
bool Remove(T item)
Removes the first occurrence of a specific object from the T:System.Collections.Generic....
Definition: List.cs:1307
override IdentityReference Translate(Type targetType)
Translates the account name represented by the T:System.Security.Principal.SecurityIdentifier object ...