stride3d/stride · error · ArgumentException

The given object is not a KeyValuePair.

Error message

The given object is not a KeyValuePair.

What it means

ReferenceEnumerable.GetKey extracts the Key part of a dictionary entry. Dictionary entries are surfaced as KeyValuePair<TKey,TValue> objects; if the passed object's runtime type is not a generic KeyValuePair<,>, there is no Key property to reflect on, so it throws ArgumentException.

Solutions

  1. Only pass items obtained from enumerating the dictionary node (they are KeyValuePair<,> instances).
  2. If you have a key directly, build a NodeIndex from it instead of routing through GetKey/key.
  3. Convert DictionaryEntry to KeyValuePair<TKey,TValue> before passing.

Example fix

// before
var idx = enumerableReference.key((DictionaryEntry)entry);
// after
var pair = new KeyValuePair<string, object>((string)entry.Key, entry.Value);
var idx = enumerableReference.key(pair);
Defensive patterns

Strategy: type-guard

Validate before calling

var t = item.GetType();
bool isKvp = t.IsGenericType && t.GetGenericTypeDefinition() == typeof(KeyValuePair<,>);
if (isKvp) var idx = reference.key(item);

Type guard

bool IsKeyValuePair(object o) => o.GetType() is { IsGenericType: true } t && t.GetGenericTypeDefinition() == typeof(KeyValuePair<,>);

Try / catch

try { var idx = reference.key(item); }
catch (ArgumentException) { /* item is not a KeyValuePair<,>; obtain it by enumerating the reference */ }

Prevention

When it happens

Trigger: Invoking the public key accessor (which calls GetKey) with an object whose type is not a closed generic KeyValuePair<,> — e.g. passing a DictionaryEntry, a tuple, or a raw key instead of the pair.

Common situations: Enumerating dictionary items manually and passing elements to reference accessors; switching from Hashtable/DictionaryEntry to Dictionary in older code; custom enumerators that yield non-KVP items.

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


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/c338d1ac309cabba. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Quantum/References/ReferenceEnumerable.cs:214

    }

    /// <inheritdoc/>
    public override string ToString()
    {
        string text = "(" + items.Count + " references";
        if (items.Count > 0)
        {
            text += ": ";
            text += string.Join(", ", items.Values);
        }
        text += ")";
        return text;
    }

    private static NodeIndex GetKey(object keyValuePair)
    {
        var type = keyValuePair.GetType();
        if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(KeyValuePair<,>)) throw new ArgumentException("The given object is not a KeyValuePair.");
        var keyProperty = type.GetProperty(nameof(KeyValuePair<object, object>.Key));
        return new NodeIndex(keyProperty?.GetValue(keyValuePair));
    }

    private static object? GetValue(object keyValuePair)
    {
        var type = keyValuePair.GetType();
        if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(KeyValuePair<,>)) throw new ArgumentException("The given object is not a KeyValuePair.");
        var valueProperty = type.GetProperty(nameof(KeyValuePair<object, object>.Value));
        return valueProperty?.GetValue(keyValuePair);
    }

    /// <summary>
    /// An enumerator for <see cref="ReferenceEnumerable"/> that enumerates in proper item order.
    /// </summary>
    public readonly struct ReferenceEnumerator : IEnumerator<ObjectReference>
    {
        private readonly IEnumerator<NodeIndex> indexEnumerator;

View on GitHub (pinned to 96fad776d2)