Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Security / Principal / IRCollection.cs / 1305376 / IRCollection.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
namespace System.Security.Principal
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Security.Permissions;
[System.Runtime.InteropServices.ComVisible(false)]
public class IdentityReferenceCollection : ICollection
{
#region Private members
//
// Container enumerated by this collection
//
private List _Identities;
#endregion
#region Constructors
//
// Creates an empty collection of default size
//
public IdentityReferenceCollection()
: this( 0 )
{
}
//
// Creates an empty collection of given initial size
//
public IdentityReferenceCollection( int capacity )
{
_Identities = new List( capacity );
}
#endregion
#region ICollection implementation
public void CopyTo( IdentityReference[] array, int offset )
{
_Identities.CopyTo( 0, array, offset, Count );
}
public int Count
{
get { return _Identities.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public void Add( IdentityReference identity )
{
if ( identity == null )
{
throw new ArgumentNullException( "identity" );
}
Contract.EndContractBlock();
_Identities.Add( identity );
}
public bool Remove( IdentityReference identity )
{
if ( identity == null )
{
throw new ArgumentNullException( "identity" );
}
Contract.EndContractBlock();
if ( Contains( identity ))
{
_Identities.Remove( identity );
return true;
}
return false;
}
public void Clear()
{
_Identities.Clear();
}
public bool Contains( IdentityReference identity )
{
if ( identity == null )
{
throw new ArgumentNullException( "identity" );
}
Contract.EndContractBlock();
return _Identities.Contains( identity );
}
#endregion
#region IEnumerable implementation
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator GetEnumerator()
{
return new IdentityReferenceEnumerator( this );
}
#endregion
#region Public methods
public IdentityReference this[int index]
{
get
{
return _Identities[index];
}
set
{
if ( value == null )
{
throw new ArgumentNullException( "value" );
}
Contract.EndContractBlock();
_Identities[index] = value;
}
}
internal List Identities
{
get { return _Identities; }
}
public IdentityReferenceCollection Translate( Type targetType )
{
return Translate( targetType, false );
}
[SecuritySafeCritical]
[SecurityPermission(SecurityAction.Demand, ControlPrincipal = true)]
public IdentityReferenceCollection Translate( Type targetType, bool forceSuccess )
{
if ( targetType == null )
{
throw new ArgumentNullException( "targetType" );
}
//
// Target type must be a subclass of IdentityReference
//
if ( !targetType.IsSubclassOf( typeof( IdentityReference )))
{
throw new ArgumentException( Environment.GetResourceString( "IdentityReference_MustBeIdentityReference" ), "targetType" );
}
Contract.EndContractBlock();
//
// if the source collection is empty, just return an empty collection
//
if (Identities.Count == 0)
{
return new IdentityReferenceCollection();
}
int SourceSidsCount = 0;
int SourceNTAccountsCount = 0;
//
// First, see how many of each of the source types we have.
// The cases where source type == target type require no conversion.
//
for ( int i = 0; i < Identities.Count; i++ )
{
Type type = Identities[i].GetType();
if ( type == targetType )
{
continue;
}
else if ( type == typeof( SecurityIdentifier ))
{
SourceSidsCount += 1;
}
else if ( type == typeof( NTAccount ))
{
SourceNTAccountsCount += 1;
}
else
{
//
// Rare case that we have defined a type of identity reference and
// not included it in the code logic above (this is more like a bug in the implementation
// but only as long as we do not allow IdentityReference to be subclassed outside of the BCL)
//
Contract.Assert( false, "Source type is an IdentityReference type which has not been included in translation logic.");
throw new SystemException();
}
}
bool Homogeneous = false;
IdentityReferenceCollection SourceSids = null;
IdentityReferenceCollection SourceNTAccounts = null;
if ( SourceSidsCount == Count )
{
Homogeneous = true;
SourceSids = this;
}
else if ( SourceSidsCount > 0 )
{
SourceSids = new IdentityReferenceCollection( SourceSidsCount );
}
if ( SourceNTAccountsCount == Count )
{
Homogeneous = true;
SourceNTAccounts = this;
}
else if ( SourceNTAccountsCount > 0 )
{
SourceNTAccounts = new IdentityReferenceCollection( SourceNTAccountsCount );
}
//
// Repackage only if the source is not homogeneous (contains different source types)
//
IdentityReferenceCollection Result = null;
if ( !Homogeneous )
{
Result = new IdentityReferenceCollection( Identities.Count );
for ( int i = 0; i < Identities.Count; i++ )
{
IdentityReference id = this[i];
Type type = id.GetType();
if ( type == targetType )
{
continue;
}
else if ( type == typeof( SecurityIdentifier ))
{
SourceSids.Add( id );
}
else if ( type == typeof( NTAccount ))
{
SourceNTAccounts.Add( id );
}
else
{
//
// Rare case that we have defined a type of identity reference and
// not included it in the code logic above (this is more like a bug in the implementation
// but only as long as we do not allow IdentityReference to be subclassed outside of the BCL)
//
Contract.Assert( false, "Source type is an IdentityReference type which has not been included in translation logic.");
throw new SystemException();
}
}
}
bool someFailed = false;
IdentityReferenceCollection TargetSids = null, TargetNTAccounts = null;
if ( SourceSidsCount > 0 )
{
TargetSids = SecurityIdentifier.Translate( SourceSids, targetType, out someFailed );
if ( Homogeneous && !(forceSuccess && someFailed))
{
Result = TargetSids;
}
}
if ( SourceNTAccountsCount > 0 )
{
TargetNTAccounts = NTAccount.Translate( SourceNTAccounts, targetType, out someFailed );
if ( Homogeneous && !(forceSuccess && someFailed))
{
Result = TargetNTAccounts;
}
}
if (forceSuccess && someFailed) {
//
// Need to throw an exception here and provide information regarding
// which identity references could not be translated to the target type
//
Result = new IdentityReferenceCollection();
if (TargetSids != null) {
foreach (IdentityReference id in TargetSids) {
if ( id.GetType() != targetType )
{
Result.Add(id);
}
}
}
if (TargetNTAccounts != null) {
foreach (IdentityReference id in TargetNTAccounts) {
if ( id.GetType() != targetType )
{
Result.Add(id);
}
}
}
throw new IdentityNotMappedException( Environment.GetResourceString("IdentityReference_IdentityNotMapped"), Result);
}
else if ( !Homogeneous )
{
SourceSidsCount = 0;
SourceNTAccountsCount = 0;
Result = new IdentityReferenceCollection( Identities.Count );
for ( int i = 0; i < Identities.Count; i++ )
{
IdentityReference id = this[i];
Type type = id.GetType();
if ( type == targetType )
{
Result.Add( id );
}
else if ( type == typeof( SecurityIdentifier ))
{
Result.Add( TargetSids[SourceSidsCount++] );
}
else if ( type == typeof( NTAccount ))
{
Result.Add( TargetNTAccounts[SourceNTAccountsCount++] );
}
else
{
//
// Rare case that we have defined a type of identity reference and
// not included it in the code logic above (this is more like a bug in the implementation
// but only as long as we do not allow IdentityReference to be subclassed outside of the BCL)
//
Contract.Assert( false, "Source type is an IdentityReference type which has not been included in translation logic.");
throw new SystemException();
}
}
}
return Result;
}
#endregion
}
[System.Runtime.InteropServices.ComVisible(false)]
internal class IdentityReferenceEnumerator : IEnumerator, IDisposable
{
#region Private members
//
// Current enumeration index
//
private int _Current;
//
// Parent collection
//
private readonly IdentityReferenceCollection _Collection;
#endregion
#region Constructors
internal IdentityReferenceEnumerator( IdentityReferenceCollection collection )
{
if ( collection == null )
{
throw new ArgumentNullException( "collection" );
}
Contract.EndContractBlock();
_Collection = collection;
_Current = -1;
}
#endregion
#region IEnumerator implementation
///
object IEnumerator.Current
{
get { return _Collection.Identities[_Current]; }
}
public IdentityReference Current
{
get { return (( IEnumerator )this).Current as IdentityReference; }
}
public bool MoveNext()
{
_Current++;
return ( _Current < _Collection.Count );
}
public void Reset()
{
_Current = -1;
}
public void Dispose()
{
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
namespace System.Security.Principal
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Security.Permissions;
[System.Runtime.InteropServices.ComVisible(false)]
public class IdentityReferenceCollection : ICollection
{
#region Private members
//
// Container enumerated by this collection
//
private List _Identities;
#endregion
#region Constructors
//
// Creates an empty collection of default size
//
public IdentityReferenceCollection()
: this( 0 )
{
}
//
// Creates an empty collection of given initial size
//
public IdentityReferenceCollection( int capacity )
{
_Identities = new List( capacity );
}
#endregion
#region ICollection implementation
public void CopyTo( IdentityReference[] array, int offset )
{
_Identities.CopyTo( 0, array, offset, Count );
}
public int Count
{
get { return _Identities.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public void Add( IdentityReference identity )
{
if ( identity == null )
{
throw new ArgumentNullException( "identity" );
}
Contract.EndContractBlock();
_Identities.Add( identity );
}
public bool Remove( IdentityReference identity )
{
if ( identity == null )
{
throw new ArgumentNullException( "identity" );
}
Contract.EndContractBlock();
if ( Contains( identity ))
{
_Identities.Remove( identity );
return true;
}
return false;
}
public void Clear()
{
_Identities.Clear();
}
public bool Contains( IdentityReference identity )
{
if ( identity == null )
{
throw new ArgumentNullException( "identity" );
}
Contract.EndContractBlock();
return _Identities.Contains( identity );
}
#endregion
#region IEnumerable implementation
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator GetEnumerator()
{
return new IdentityReferenceEnumerator( this );
}
#endregion
#region Public methods
public IdentityReference this[int index]
{
get
{
return _Identities[index];
}
set
{
if ( value == null )
{
throw new ArgumentNullException( "value" );
}
Contract.EndContractBlock();
_Identities[index] = value;
}
}
internal List Identities
{
get { return _Identities; }
}
public IdentityReferenceCollection Translate( Type targetType )
{
return Translate( targetType, false );
}
[SecuritySafeCritical]
[SecurityPermission(SecurityAction.Demand, ControlPrincipal = true)]
public IdentityReferenceCollection Translate( Type targetType, bool forceSuccess )
{
if ( targetType == null )
{
throw new ArgumentNullException( "targetType" );
}
//
// Target type must be a subclass of IdentityReference
//
if ( !targetType.IsSubclassOf( typeof( IdentityReference )))
{
throw new ArgumentException( Environment.GetResourceString( "IdentityReference_MustBeIdentityReference" ), "targetType" );
}
Contract.EndContractBlock();
//
// if the source collection is empty, just return an empty collection
//
if (Identities.Count == 0)
{
return new IdentityReferenceCollection();
}
int SourceSidsCount = 0;
int SourceNTAccountsCount = 0;
//
// First, see how many of each of the source types we have.
// The cases where source type == target type require no conversion.
//
for ( int i = 0; i < Identities.Count; i++ )
{
Type type = Identities[i].GetType();
if ( type == targetType )
{
continue;
}
else if ( type == typeof( SecurityIdentifier ))
{
SourceSidsCount += 1;
}
else if ( type == typeof( NTAccount ))
{
SourceNTAccountsCount += 1;
}
else
{
//
// Rare case that we have defined a type of identity reference and
// not included it in the code logic above (this is more like a bug in the implementation
// but only as long as we do not allow IdentityReference to be subclassed outside of the BCL)
//
Contract.Assert( false, "Source type is an IdentityReference type which has not been included in translation logic.");
throw new SystemException();
}
}
bool Homogeneous = false;
IdentityReferenceCollection SourceSids = null;
IdentityReferenceCollection SourceNTAccounts = null;
if ( SourceSidsCount == Count )
{
Homogeneous = true;
SourceSids = this;
}
else if ( SourceSidsCount > 0 )
{
SourceSids = new IdentityReferenceCollection( SourceSidsCount );
}
if ( SourceNTAccountsCount == Count )
{
Homogeneous = true;
SourceNTAccounts = this;
}
else if ( SourceNTAccountsCount > 0 )
{
SourceNTAccounts = new IdentityReferenceCollection( SourceNTAccountsCount );
}
//
// Repackage only if the source is not homogeneous (contains different source types)
//
IdentityReferenceCollection Result = null;
if ( !Homogeneous )
{
Result = new IdentityReferenceCollection( Identities.Count );
for ( int i = 0; i < Identities.Count; i++ )
{
IdentityReference id = this[i];
Type type = id.GetType();
if ( type == targetType )
{
continue;
}
else if ( type == typeof( SecurityIdentifier ))
{
SourceSids.Add( id );
}
else if ( type == typeof( NTAccount ))
{
SourceNTAccounts.Add( id );
}
else
{
//
// Rare case that we have defined a type of identity reference and
// not included it in the code logic above (this is more like a bug in the implementation
// but only as long as we do not allow IdentityReference to be subclassed outside of the BCL)
//
Contract.Assert( false, "Source type is an IdentityReference type which has not been included in translation logic.");
throw new SystemException();
}
}
}
bool someFailed = false;
IdentityReferenceCollection TargetSids = null, TargetNTAccounts = null;
if ( SourceSidsCount > 0 )
{
TargetSids = SecurityIdentifier.Translate( SourceSids, targetType, out someFailed );
if ( Homogeneous && !(forceSuccess && someFailed))
{
Result = TargetSids;
}
}
if ( SourceNTAccountsCount > 0 )
{
TargetNTAccounts = NTAccount.Translate( SourceNTAccounts, targetType, out someFailed );
if ( Homogeneous && !(forceSuccess && someFailed))
{
Result = TargetNTAccounts;
}
}
if (forceSuccess && someFailed) {
//
// Need to throw an exception here and provide information regarding
// which identity references could not be translated to the target type
//
Result = new IdentityReferenceCollection();
if (TargetSids != null) {
foreach (IdentityReference id in TargetSids) {
if ( id.GetType() != targetType )
{
Result.Add(id);
}
}
}
if (TargetNTAccounts != null) {
foreach (IdentityReference id in TargetNTAccounts) {
if ( id.GetType() != targetType )
{
Result.Add(id);
}
}
}
throw new IdentityNotMappedException( Environment.GetResourceString("IdentityReference_IdentityNotMapped"), Result);
}
else if ( !Homogeneous )
{
SourceSidsCount = 0;
SourceNTAccountsCount = 0;
Result = new IdentityReferenceCollection( Identities.Count );
for ( int i = 0; i < Identities.Count; i++ )
{
IdentityReference id = this[i];
Type type = id.GetType();
if ( type == targetType )
{
Result.Add( id );
}
else if ( type == typeof( SecurityIdentifier ))
{
Result.Add( TargetSids[SourceSidsCount++] );
}
else if ( type == typeof( NTAccount ))
{
Result.Add( TargetNTAccounts[SourceNTAccountsCount++] );
}
else
{
//
// Rare case that we have defined a type of identity reference and
// not included it in the code logic above (this is more like a bug in the implementation
// but only as long as we do not allow IdentityReference to be subclassed outside of the BCL)
//
Contract.Assert( false, "Source type is an IdentityReference type which has not been included in translation logic.");
throw new SystemException();
}
}
}
return Result;
}
#endregion
}
[System.Runtime.InteropServices.ComVisible(false)]
internal class IdentityReferenceEnumerator : IEnumerator, IDisposable
{
#region Private members
//
// Current enumeration index
//
private int _Current;
//
// Parent collection
//
private readonly IdentityReferenceCollection _Collection;
#endregion
#region Constructors
internal IdentityReferenceEnumerator( IdentityReferenceCollection collection )
{
if ( collection == null )
{
throw new ArgumentNullException( "collection" );
}
Contract.EndContractBlock();
_Collection = collection;
_Current = -1;
}
#endregion
#region IEnumerator implementation
///
object IEnumerator.Current
{
get { return _Collection.Identities[_Current]; }
}
public IdentityReference Current
{
get { return (( IEnumerator )this).Current as IdentityReference; }
}
public bool MoveNext()
{
_Current++;
return ( _Current < _Collection.Count );
}
public void Reset()
{
_Current = -1;
}
public void Dispose()
{
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TreeNodeMouseHoverEvent.cs
- DataView.cs
- ImmComposition.cs
- ProcessModuleCollection.cs
- ConfigurationValidatorAttribute.cs
- Activity.cs
- SafeBuffer.cs
- TextParagraphProperties.cs
- OutOfProcStateClientManager.cs
- ScriptingScriptResourceHandlerSection.cs
- QuadTree.cs
- InternalPermissions.cs
- AutoScrollExpandMessageFilter.cs
- ContextMenuAutomationPeer.cs
- ExpandedProjectionNode.cs
- WebBrowserUriTypeConverter.cs
- TouchFrameEventArgs.cs
- ElementsClipboardData.cs
- SqlCaseSimplifier.cs
- CrossSiteScriptingValidation.cs
- RoleManagerSection.cs
- TreeNodeEventArgs.cs
- SqlPersonalizationProvider.cs
- KeyValuePairs.cs
- SiteMapDataSourceView.cs
- DragEventArgs.cs
- FullTrustAssemblyCollection.cs
- Util.cs
- EntityModelBuildProvider.cs
- WebPartCloseVerb.cs
- ClientConfigurationHost.cs
- MatrixConverter.cs
- ResourceProviderFactory.cs
- AddInController.cs
- SimpleHandlerBuildProvider.cs
- ImportContext.cs
- FormView.cs
- ConfigurationConverterBase.cs
- Form.cs
- IgnorePropertiesAttribute.cs
- OLEDB_Util.cs
- WebPartExportVerb.cs
- OleDbParameterCollection.cs
- _UriSyntax.cs
- WebHostScriptMappingsInstallComponent.cs
- MD5CryptoServiceProvider.cs
- MessageQueuePermissionEntry.cs
- StretchValidation.cs
- AppDomainManager.cs
- ProviderSettings.cs
- QueryOutputWriter.cs
- FlowDocumentPage.cs
- EpmContentSerializer.cs
- RelatedView.cs
- FtpRequestCacheValidator.cs
- XsltContext.cs
- BindableTemplateBuilder.cs
- VectorValueSerializer.cs
- PointCollection.cs
- WebBrowser.cs
- RoleBoolean.cs
- InkPresenter.cs
- AcceptorSessionSymmetricMessageSecurityProtocol.cs
- InternalControlCollection.cs
- WaitHandleCannotBeOpenedException.cs
- IEnumerable.cs
- validation.cs
- Matrix.cs
- shaperfactoryquerycacheentry.cs
- TextEffect.cs
- TemplateManager.cs
- StructuralType.cs
- JsonServiceDocumentSerializer.cs
- ClaimSet.cs
- BamlWriter.cs
- SqlParameterizer.cs
- PropertyEntry.cs
- StdRegProviderWrapper.cs
- XPathDescendantIterator.cs
- LookupBindingPropertiesAttribute.cs
- SynchronizationValidator.cs
- ImageInfo.cs
- FrameworkContentElementAutomationPeer.cs
- Lazy.cs
- ScrollViewerAutomationPeer.cs
- _Connection.cs
- EdmSchemaAttribute.cs
- FormViewPagerRow.cs
- HtmlValidationSummaryAdapter.cs
- DynamicILGenerator.cs
- TaskCanceledException.cs
- TrustManager.cs
- QueryHandler.cs
- BindValidationContext.cs
- SerializationIncompleteException.cs
- CodeGenerationManager.cs
- XmlSchemaException.cs
- RegexParser.cs
- ToolBarPanel.cs
- XmlSchemaSimpleContentRestriction.cs