dotnet/wpf · error · ArgumentException
SR.CanNotCompareDiffTypes
Error message
SR.CanNotCompareDiffTypes
What it means
CompoundFileStorageReference.CompareTo(object) requires its argument to be another CompoundFileStorageReference. When passed a non-null object of a different type it throws ArgumentException with SR.CanNotCompareDiffTypes. Comparisons are done ordinal-insensitively on the upper-invariant full name, which only makes sense between same-type references.
Solutions
- Ensure every element compared is a CompoundFileStorageReference; filter or partition mixed collections before sorting.
- Use type-safe generic collections (List<CompoundFileStorageReference>) so the compiler enforces element type.
- Compare against null first if you want the standard 'any reference sorts after null' behavior (CompareTo already returns 1 for null).
- If comparing across reference kinds, compare on a shared key (e.g. FullName string) instead of CompareTo.
Example fix
// before list.Contains(mixedObject); // mixed List<object> sorted -> ArgumentException // after var storages = list.OfType<CompoundFileStorageReference>().ToList(); storages.Sort(); // only same-type references compared
Defensive patterns
Strategy: type-guard
Validate before calling
if (other is CompoundFileStorageReference) { /* safe to sort */ } Type guard
bool IsComparableStorageRef(object o) => o is CompoundFileStorageReference;
Try / catch
try { result = storageRef.CompareTo(obj); }
catch (ArgumentException) { result = string.CompareOrdinal(storageRef.FullName, obj?.ToString()); } Prevention
- Use strongly typed collections instead of ArrayList/object[]
- Filter with OfType<T>() before sorting
- Never mix storage and stream references in one sorted list
When it happens
Trigger: Calling CompareTo on a CompoundFileStorageReference and passing an object of any other type (e.g. a CompoundFileStreamReference, string, or unrelated class); also occurs indirectly via Sort()/ArrayList.Sort on mixed-type collections.
Common situations: Sorting mixed lists of storage and stream references in custom packaging tools, comparing references from different compound-file implementations, or passing parsed strings instead of reference objects.
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
- Cannot compare different types.
- ' ' 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/3be9a1fa40637bd7.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileStorageReference.cs:85
#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</returns>
int IComparable.CompareTo(object o)
{
if (o == null)
return 1; // Standard behavior.
// different type?
if (o.GetType() != GetType())
throw new ArgumentException(
SR.CanNotCompareDiffTypes);
// Note that because of the GetType() checking above, the casting must be valid.
CompoundFileStorageReference r = (CompoundFileStorageReference)o;
return String.CompareOrdinal(_fullName.ToUpperInvariant(), r._fullName.ToUpperInvariant());
}
#endregion
//------------------------------------------------------
//
// Private Methods
//
//------------------------------------------------------
/// <summary>
/// Assign the fullName
/// </summary>
/// <param name="fullName">name</param>
/// <remarks>cache a duplicate copy of the storage name to save having to do this for View on GitHub (pinned to 81131a70a4)