Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / 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(); } } } // 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; 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- GridViewRow.cs
- MessageUtil.cs
- EntityViewGenerationAttribute.cs
- PolicyValidationException.cs
- SelectionPatternIdentifiers.cs
- XPathMultyIterator.cs
- DeclarativeCatalogPartDesigner.cs
- ActivityBuilderXamlWriter.cs
- UrlAuthorizationModule.cs
- DataKey.cs
- SqlTypesSchemaImporter.cs
- FixedPageAutomationPeer.cs
- StylusCaptureWithinProperty.cs
- TextEditorSpelling.cs
- PropertyTab.cs
- DetailsViewPageEventArgs.cs
- WebPartDeleteVerb.cs
- VirtualizingStackPanel.cs
- PatternMatcher.cs
- InvokeMethodDesigner.xaml.cs
- ContractReference.cs
- TextEffectResolver.cs
- InternalMappingException.cs
- DelegateSerializationHolder.cs
- Tag.cs
- XmlSchemaImport.cs
- TextBoxDesigner.cs
- CodeMethodMap.cs
- DataGridRow.cs
- TimeSpanSecondsConverter.cs
- ObfuscationAttribute.cs
- Component.cs
- ExtensionSimplifierMarkupObject.cs
- Compilation.cs
- ActiveXHost.cs
- IUnknownConstantAttribute.cs
- WeakReferenceEnumerator.cs
- KeyEvent.cs
- ResXFileRef.cs
- DataGridParentRows.cs
- InternalSendMessage.cs
- RegisteredExpandoAttribute.cs
- PrintPreviewGraphics.cs
- CapabilitiesRule.cs
- Publisher.cs
- SystemIPv6InterfaceProperties.cs
- CallSiteBinder.cs
- Table.cs
- HtmlTableRow.cs
- DLinqTableProvider.cs
- StateMachineSubscriptionManager.cs
- OrderedDictionary.cs
- DecimalConstantAttribute.cs
- MultiAsyncResult.cs
- Cursor.cs
- EncryptedXml.cs
- DataSourceCache.cs
- XmlQueryOutput.cs
- XamlPointCollectionSerializer.cs
- BufferModeSettings.cs
- ReferenceList.cs
- InfoCardPolicy.cs
- SqlNotificationRequest.cs
- MetadataSerializer.cs
- VersionConverter.cs
- HelpPage.cs
- XmlResolver.cs
- DesignerDataTable.cs
- SystemWebSectionGroup.cs
- UInt64.cs
- RangeBase.cs
- TabControlAutomationPeer.cs
- WhiteSpaceTrimStringConverter.cs
- PermissionAttributes.cs
- X509WindowsSecurityToken.cs
- MouseWheelEventArgs.cs
- XmlAttributeCollection.cs
- WebConfigurationFileMap.cs
- HTMLTagNameToTypeMapper.cs
- HtmlSelect.cs
- ThousandthOfEmRealPoints.cs
- BaseComponentEditor.cs
- _PooledStream.cs
- CopyNodeSetAction.cs
- brushes.cs
- SchemaTableColumn.cs
- IMembershipProvider.cs
- DesignerActionUI.cs
- HttpModule.cs
- SortQuery.cs
- XmlRootAttribute.cs
- SHA512.cs
- AmbientProperties.cs
- SqlNode.cs
- PolicyStatement.cs
- AssemblyNameEqualityComparer.cs
- WindowHideOrCloseTracker.cs
- TableRowsCollectionEditor.cs
- RegexTree.cs
- TextEditorSpelling.cs