dotnet/wpf · error · ArgumentException
Cannot compare different types.
Error message
Cannot compare different types.
What it means
CompoundFileStreamReference.CompareTo(object) only supports comparing against another CompoundFileStreamReference. Passing any other non-null object type throws ArgumentException (SR.CanNotCompareDiffTypes). The comparison itself is an ordinal compare of the upper-invariant full names.
Solutions
- Keep stream references in a strongly typed List<CompoundFileStreamReference> before sorting.
- Separate storages and streams into different collections and sort each independently.
- Filter with OfType<CompoundFileStreamReference>() before any Sort/CompareTo.
- If a cross-type ordering is needed, implement a custom comparer over a common property like FullName.
Example fix
// before ArrayList refs = GetMixedReferences(); refs.Sort(); // ArgumentException on mixed types // after var streamRefs = GetMixedReferences().OfType<CompoundFileStreamReference>().ToList(); streamRefs.Sort();
Defensive patterns
Strategy: type-guard
Validate before calling
if (other is CompoundFileStreamReference) { /* safe to compare */ } Type guard
bool IsComparableStreamRef(object o) => o is CompoundFileStreamReference;
Try / catch
try { result = streamRef.CompareTo(obj); }
catch (ArgumentException) { result = -1; /* or custom cross-type ordering */ } Prevention
- Keep stream references in List<CompoundFileStreamReference>
- Sort storages and streams in separate collections
- Use a custom IComparer over FullName for mixed-type ordering
When it happens
Trigger: Calling CompareTo on a CompoundFileStreamReference with a non-null argument of a different type — e.g. a CompoundFileStorageReference, a string, or an element of an object[]/ArrayList during Sort.
Common situations: Sorting heterogeneous reference collections in packaging utilities, mixing stream and storage references in one list, or accidentally boxing/typing arguments as object.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.CanNotCompareDiffTypes
- ' ' ID is not a valid XSD ID.
- Cannot create a read-only stream.
- Cannot have leading path delimiter.
- CompoundFile path must be non-empty.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/57ade77f6b95fe71.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileStreamReference.cs:106
#endregion
#region IComparable
/// <summary>
/// Compares two CompoundFileReferences
/// </summary>
/// <param name="o">CompoundFileReference to compare to this one</param>
/// <remarks>Supports the IComparable interface</remarks>
/// <returns>Less than zero if this instance is less than the given reference, zero if they are equal
/// and greater than zero if this instance is greater than the given reference. Not case sensitive.</returns>
int IComparable.CompareTo(object o)
{
if (o == null)
return 1; // Standard behavior.
// different type?
if (o.GetType() != GetType())
throw new ArgumentException(
SR.CanNotCompareDiffTypes);
CompoundFileStreamReference r = (CompoundFileStreamReference)o;
return String.CompareOrdinal(_fullName.ToUpperInvariant(), r._fullName.ToUpperInvariant());
}
#endregion
//------------------------------------------------------
//
// Private Methods
//
//------------------------------------------------------
/// <summary>
/// Initialize _fullName
/// </summary>
/// <remarks>this should only be called from constructors as references are immutable</remarks>
/// <param name="fullName">string to parse</param>
/// <exception cref="ArgumentException">if leading or trailing path delimiter</exception>View on GitHub (pinned to 81131a70a4)