Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / clr / src / BCL / System / Collections / Generic / Comparer.cs / 1 / Comparer.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== using System; using System.Collections; using System.Collections.Generic; namespace System.Collections.Generic { using System.Globalization; using System.Runtime.CompilerServices; [Serializable()] [TypeDependencyAttribute("System.Collections.Generic.GenericComparer`1")] public abstract class Comparer: IComparer, IComparer { static Comparer defaultComparer; public static Comparer Default { get { Comparer comparer = defaultComparer; if (comparer == null) { comparer = CreateComparer(); defaultComparer = comparer; } return comparer; } } private static Comparer CreateComparer() { Type t = typeof(T); // If T implements IComparable return a GenericComparer if (typeof(IComparable ).IsAssignableFrom(t)) { //return (Comparer )Activator.CreateInstance(typeof(GenericComparer<>).MakeGenericType(t)); return (Comparer )(typeof(GenericComparer ).TypeHandle.CreateInstanceForAnotherGenericParameter(t)); } // If T is a Nullable where U implements IComparable return a NullableComparer if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) { Type u = t.GetGenericArguments()[0]; if (typeof(IComparable<>).MakeGenericType(u).IsAssignableFrom(u)) { //return (Comparer )Activator.CreateInstance(typeof(NullableComparer<>).MakeGenericType(u)); return (Comparer )(typeof(NullableComparer ).TypeHandle.CreateInstanceForAnotherGenericParameter(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(); } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- LocalFileSettingsProvider.cs
- DrawToolTipEventArgs.cs
- ProgressBar.cs
- SqlDataSourceView.cs
- GridErrorDlg.cs
- InsufficientMemoryException.cs
- PageParserFilter.cs
- DockProviderWrapper.cs
- MD5.cs
- Aggregates.cs
- EncodingDataItem.cs
- BCLDebug.cs
- SmiMetaData.cs
- CallTemplateAction.cs
- CodeAccessSecurityEngine.cs
- TypeNameConverter.cs
- DbParameterCollection.cs
- DataControlImageButton.cs
- PageBuildProvider.cs
- Calendar.cs
- ProcessDesigner.cs
- CompileXomlTask.cs
- Message.cs
- AppDomainFactory.cs
- ByteStreamGeometryContext.cs
- Currency.cs
- Atom10FormatterFactory.cs
- SoapExtensionReflector.cs
- DataBindingCollectionConverter.cs
- WebPartConnectionsCloseVerb.cs
- CachingParameterInspector.cs
- TypeDescriptionProviderAttribute.cs
- BindingManagerDataErrorEventArgs.cs
- StrokeIntersection.cs
- ReadWriteSpinLock.cs
- PluggableProtocol.cs
- PeerTransportSecurityElement.cs
- ISAPIRuntime.cs
- ByeOperation11AsyncResult.cs
- EdmSchemaError.cs
- UriSection.cs
- PropertyPathWorker.cs
- EmptyEnumerator.cs
- TreeViewHitTestInfo.cs
- SerializationAttributes.cs
- HMACSHA384.cs
- HttpServerChannel.cs
- XmlBoundElement.cs
- XslException.cs
- OracleEncoding.cs
- ShapingEngine.cs
- Animatable.cs
- SimpleColumnProvider.cs
- Resources.Designer.cs
- DnsPermission.cs
- ListViewAutomationPeer.cs
- HtmlHead.cs
- RSAPKCS1SignatureDeformatter.cs
- _SslSessionsCache.cs
- MultipleViewProviderWrapper.cs
- MailWebEventProvider.cs
- XmlTextEncoder.cs
- SafeHandles.cs
- FixedSOMGroup.cs
- ScrollViewerAutomationPeer.cs
- CapabilitiesSection.cs
- _AuthenticationState.cs
- Section.cs
- ValidationEventArgs.cs
- Rethrow.cs
- CharUnicodeInfo.cs
- ClrProviderManifest.cs
- InputProcessorProfilesLoader.cs
- IdentityNotMappedException.cs
- UpdateRecord.cs
- Task.cs
- DefaultAssemblyResolver.cs
- XMLSyntaxException.cs
- WebServiceReceive.cs
- FieldAccessException.cs
- ThicknessAnimation.cs
- _KerberosClient.cs
- XmlComplianceUtil.cs
- ImageConverter.cs
- NamedElement.cs
- XmlTypeAttribute.cs
- TextFragmentEngine.cs
- IssuedTokenClientBehaviorsElement.cs
- TabControl.cs
- SafeSerializationManager.cs
- CodeVariableDeclarationStatement.cs
- CompositeControl.cs
- TextUtf8RawTextWriter.cs
- AttributeInfo.cs
- DecoderReplacementFallback.cs
- ParameterModifier.cs
- SapiRecoContext.cs
- ToolboxDataAttribute.cs
- Path.cs
- SiteMapNodeCollection.cs