Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Collections / Generic / Comparer.cs / 1305376 / Comparer.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== //[....] // using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.Contracts; //using System.Globalization; using System.Runtime.CompilerServices; namespace System.Collections.Generic { [Serializable] [TypeDependencyAttribute("System.Collections.Generic.GenericComparer`1")] public abstract class Comparer: IComparer, IComparer { static Comparer defaultComparer; public static Comparer Default { [System.Security.SecuritySafeCritical] // auto-generated get { Contract.Ensures(Contract.Result >() != null); Comparer comparer = defaultComparer; if (comparer == null) { comparer = CreateComparer(); defaultComparer = comparer; } return comparer; } } [System.Security.SecuritySafeCritical] // auto-generated private static Comparer CreateComparer() { RuntimeType t = (RuntimeType)typeof(T); // If T implements IComparable return a GenericComparer if (typeof(IComparable ).IsAssignableFrom(t)) { //return (Comparer )Activator.CreateInstance(typeof(GenericComparer<>).MakeGenericType(t)); return (Comparer )RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer ), t); } // If T is a Nullable where U implements IComparable return a NullableComparer if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) { RuntimeType u = (RuntimeType)t.GetGenericArguments()[0]; if (typeof(IComparable<>).MakeGenericType(u).IsAssignableFrom(u)) { //return (Comparer )Activator.CreateInstance(typeof(NullableComparer<>).MakeGenericType(u)); return (Comparer )RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer ), u); } } // Otherwise return an ObjectComparer return new ObjectComparer (); } public abstract int Compare(T x, T y); int IComparer.Compare(object x, object y) { if (x == null) return y == null ? 0 : -1; if (y == null) return 1; if (x is T && y is T) return Compare((T)x, (T)y); ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArgumentForComparison); return 0; } } [Serializable] internal class GenericComparer : Comparer where T: IComparable { public override int Compare(T x, T y) { if (x != null) { if (y != null) return x.CompareTo(y); return 1; } if (y != null) return -1; return 0; } // Equals method for the comparer itself. public override bool Equals(Object obj){ GenericComparer comparer = obj as GenericComparer ; return comparer != null; } public override int GetHashCode() { return this.GetType().Name.GetHashCode(); } } [Serializable] internal class NullableComparer : Comparer > where T : struct, IComparable { public override int Compare(Nullable x, Nullable y) { if (x.HasValue) { if (y.HasValue) return x.value.CompareTo(y.value); return 1; } if (y.HasValue) return -1; return 0; } // Equals method for the comparer itself. public override bool Equals(Object obj){ NullableComparer comparer = obj as NullableComparer ; return comparer != null; } public override int GetHashCode() { return this.GetType().Name.GetHashCode(); } } [Serializable] internal class ObjectComparer : Comparer { public override int Compare(T x, T y) { return System.Collections.Comparer.Default.Compare(x, y); } // Equals method for the comparer itself. public override bool Equals(Object obj){ ObjectComparer comparer = obj as ObjectComparer ; return comparer != null; } public override int GetHashCode() { return this.GetType().Name.GetHashCode(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // [....] // using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.Contracts; //using System.Globalization; using System.Runtime.CompilerServices; namespace System.Collections.Generic { [Serializable] [TypeDependencyAttribute("System.Collections.Generic.GenericComparer`1")] public abstract class Comparer: IComparer, IComparer { static Comparer defaultComparer; public static Comparer Default { [System.Security.SecuritySafeCritical] // auto-generated get { Contract.Ensures(Contract.Result >() != null); Comparer comparer = defaultComparer; if (comparer == null) { comparer = CreateComparer(); defaultComparer = comparer; } return comparer; } } [System.Security.SecuritySafeCritical] // auto-generated private static Comparer CreateComparer() { RuntimeType t = (RuntimeType)typeof(T); // If T implements IComparable return a GenericComparer if (typeof(IComparable ).IsAssignableFrom(t)) { //return (Comparer )Activator.CreateInstance(typeof(GenericComparer<>).MakeGenericType(t)); return (Comparer )RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer ), t); } // If T is a Nullable where U implements IComparable return a NullableComparer if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) { RuntimeType u = (RuntimeType)t.GetGenericArguments()[0]; if (typeof(IComparable<>).MakeGenericType(u).IsAssignableFrom(u)) { //return (Comparer )Activator.CreateInstance(typeof(NullableComparer<>).MakeGenericType(u)); return (Comparer )RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer ), u); } } // Otherwise return an ObjectComparer return new ObjectComparer (); } public abstract int Compare(T x, T y); int IComparer.Compare(object x, object y) { if (x == null) return y == null ? 0 : -1; if (y == null) return 1; if (x is T && y is T) return Compare((T)x, (T)y); ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArgumentForComparison); return 0; } } [Serializable] internal class GenericComparer : Comparer where T: IComparable { public override int Compare(T x, T y) { if (x != null) { if (y != null) return x.CompareTo(y); return 1; } if (y != null) return -1; return 0; } // Equals method for the comparer itself. public override bool Equals(Object obj){ GenericComparer comparer = obj as GenericComparer ; return comparer != null; } public override int GetHashCode() { return this.GetType().Name.GetHashCode(); } } [Serializable] internal class NullableComparer : Comparer > where T : struct, IComparable { public override int Compare(Nullable x, Nullable y) { if (x.HasValue) { if (y.HasValue) return x.value.CompareTo(y.value); return 1; } if (y.HasValue) return -1; return 0; } // Equals method for the comparer itself. public override bool Equals(Object obj){ NullableComparer comparer = obj as NullableComparer ; return comparer != null; } public override int GetHashCode() { return this.GetType().Name.GetHashCode(); } } [Serializable] internal class ObjectComparer : Comparer { public override int Compare(T x, T y) { return System.Collections.Comparer.Default.Compare(x, y); } // Equals method for the comparer itself. public override bool Equals(Object obj){ ObjectComparer comparer = obj as ObjectComparer ; return comparer != null; } public override int GetHashCode() { return this.GetType().Name.GetHashCode(); } } } // 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
- HostedTransportConfigurationManager.cs
- TTSEngineProxy.cs
- SystemIdentity.cs
- CodeNamespace.cs
- DataContractSerializerOperationBehavior.cs
- KoreanCalendar.cs
- COM2IManagedPerPropertyBrowsingHandler.cs
- DataBinding.cs
- StatusBarDesigner.cs
- WebException.cs
- StateManagedCollection.cs
- Metadata.cs
- ConnectionManagementElementCollection.cs
- XPathSelfQuery.cs
- ComponentSerializationService.cs
- DBConnectionString.cs
- BitmapEffectGroup.cs
- ExceptionHandlerDesigner.cs
- ADMembershipUser.cs
- WSHttpBindingElement.cs
- TextMessageEncodingBindingElement.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- ApplicationInfo.cs
- CodeTypeReference.cs
- ResumeStoryboard.cs
- Schema.cs
- AssociatedControlConverter.cs
- StrokeIntersection.cs
- DataViewListener.cs
- FlowNode.cs
- HttpApplicationStateBase.cs
- Stroke.cs
- CodeAttributeArgumentCollection.cs
- StandardBindingElement.cs
- WebScriptEnablingBehavior.cs
- Span.cs
- ExceptionValidationRule.cs
- LogLogRecordHeader.cs
- Viewport3DVisual.cs
- IgnoreDeviceFilterElementCollection.cs
- AssemblyBuilder.cs
- SchemaImporterExtensionElementCollection.cs
- DeflateStreamAsyncResult.cs
- StoreAnnotationsMap.cs
- ViewStateException.cs
- DesignerTransactionCloseEvent.cs
- SAPIEngineTypes.cs
- ItemsPresenter.cs
- SignatureHelper.cs
- CalendarItem.cs
- RowToFieldTransformer.cs
- IndexerNameAttribute.cs
- EditBehavior.cs
- CanonicalFormWriter.cs
- RightsManagementEncryptionTransform.cs
- FormsIdentity.cs
- SimpleTableProvider.cs
- DocumentOrderQuery.cs
- DataContractSerializerServiceBehavior.cs
- SiteMapPath.cs
- ColorDialog.cs
- JsonGlobals.cs
- WhileDesigner.xaml.cs
- WinHttpWebProxyFinder.cs
- VBCodeProvider.cs
- MergeLocalizationDirectives.cs
- TranslateTransform.cs
- ApplicationSecurityManager.cs
- DelimitedListTraceListener.cs
- FieldNameLookup.cs
- ActivityDesignerResources.cs
- SqlWebEventProvider.cs
- CircleHotSpot.cs
- MbpInfo.cs
- TrackingStringDictionary.cs
- ProcessActivityTreeOptions.cs
- NameNode.cs
- XmlSchemaInclude.cs
- DataSourceConverter.cs
- DataGridViewImageCell.cs
- FileDialog.cs
- NativeCppClassAttribute.cs
- BitmapEffectInputConnector.cs
- SystemDiagnosticsSection.cs
- ReferencedCollectionType.cs
- Int64AnimationBase.cs
- HostedHttpRequestAsyncResult.cs
- CustomLineCap.cs
- XslCompiledTransform.cs
- TrackingLocationCollection.cs
- TouchEventArgs.cs
- CryptoConfig.cs
- RsaElement.cs
- Comparer.cs
- _NestedSingleAsyncResult.cs
- EmptyControlCollection.cs
- storepermission.cs
- ListCardsInFileRequest.cs
- BitmapCodecInfoInternal.cs
- DataGridItemEventArgs.cs