Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / ndp / clr / src / BCL / System / Collections / Generic / Comparer.cs / 2 / 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(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ResXBuildProvider.cs
- OpenTypeLayout.cs
- MaskDescriptors.cs
- SpellCheck.cs
- TableItemProviderWrapper.cs
- Deflater.cs
- ExtensionSimplifierMarkupObject.cs
- MenuAdapter.cs
- Assembly.cs
- Button.cs
- NamespaceCollection.cs
- AsyncResult.cs
- ScrollEvent.cs
- ProjectedWrapper.cs
- MdiWindowListStrip.cs
- ObjectQuery.cs
- ExpressionsCollectionEditor.cs
- CodeDelegateCreateExpression.cs
- PkcsUtils.cs
- DrawingState.cs
- DataGridAutoFormat.cs
- ShaderEffect.cs
- IpcChannelHelper.cs
- WebPartVerb.cs
- UnauthorizedWebPart.cs
- StringFreezingAttribute.cs
- WorkflowInstanceRecord.cs
- MatcherBuilder.cs
- DataTableReaderListener.cs
- WeakRefEnumerator.cs
- TextElement.cs
- IgnoreFileBuildProvider.cs
- Ipv6Element.cs
- LifetimeServices.cs
- DefaultMemberAttribute.cs
- HttpCapabilitiesBase.cs
- GroupQuery.cs
- SqlClientWrapperSmiStreamChars.cs
- XmlSerializer.cs
- OleDbConnectionInternal.cs
- X509SecurityTokenParameters.cs
- coordinatorfactory.cs
- LinearGradientBrush.cs
- GeometryDrawing.cs
- Accessors.cs
- EntityTypeEmitter.cs
- Int16Animation.cs
- DefaultHttpHandler.cs
- TransferRequestHandler.cs
- ListItemsCollectionEditor.cs
- TableLayoutRowStyleCollection.cs
- HtmlInputFile.cs
- FormsAuthenticationUserCollection.cs
- InvokeMethodActivity.cs
- ZipIOExtraFieldZip64Element.cs
- WebBrowserUriTypeConverter.cs
- WindowsRichEdit.cs
- EventLogPermissionEntry.cs
- MarkupCompilePass1.cs
- NameValuePermission.cs
- SystemWebExtensionsSectionGroup.cs
- Encoder.cs
- NullableBoolConverter.cs
- ListControlDesigner.cs
- FreeFormPanel.cs
- FormatConvertedBitmap.cs
- EnvelopedSignatureTransform.cs
- LogArchiveSnapshot.cs
- ExtendedPropertyCollection.cs
- BuildDependencySet.cs
- TextStore.cs
- CustomError.cs
- AppDomain.cs
- MatrixKeyFrameCollection.cs
- MenuItemBinding.cs
- LocatorManager.cs
- HtmlTableRow.cs
- altserialization.cs
- IERequestCache.cs
- Color.cs
- AnnotationResourceCollection.cs
- DataGridBoolColumn.cs
- StrokeCollection2.cs
- AssemblyAssociatedContentFileAttribute.cs
- TextEffect.cs
- FormatterServices.cs
- PersonalizationStateInfo.cs
- HtmlEmptyTagControlBuilder.cs
- ExtenderHelpers.cs
- TagElement.cs
- SelectorItemAutomationPeer.cs
- CustomSignedXml.cs
- TreeNodeStyle.cs
- GeneralTransform3D.cs
- HandleRef.cs
- HtmlForm.cs
- TokenizerHelper.cs
- TextureBrush.cs
- CrossAppDomainChannel.cs
- XmlAnyAttributeAttribute.cs