{"record":{"id":"c338d1ac309cabba","repo":"stride3d/stride","slug":"the-given-object-is-not-a-keyvaluepair","errorCode":null,"errorMessage":"The given object is not a KeyValuePair.","messagePattern":"The given object is not a KeyValuePair\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Quantum/References/ReferenceEnumerable.cs","lineNumber":214,"sourceCode":"    }\n\n    /// <inheritdoc/>\n    public override string ToString()\n    {\n        string text = \"(\" + items.Count + \" references\";\n        if (items.Count > 0)\n        {\n            text += \": \";\n            text += string.Join(\", \", items.Values);\n        }\n        text += \")\";\n        return text;\n    }\n\n    private static NodeIndex GetKey(object keyValuePair)\n    {\n        var type = keyValuePair.GetType();\n        if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(KeyValuePair<,>)) throw new ArgumentException(\"The given object is not a KeyValuePair.\");\n        var keyProperty = type.GetProperty(nameof(KeyValuePair<object, object>.Key));\n        return new NodeIndex(keyProperty?.GetValue(keyValuePair));\n    }\n\n    private static object? GetValue(object keyValuePair)\n    {\n        var type = keyValuePair.GetType();\n        if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(KeyValuePair<,>)) throw new ArgumentException(\"The given object is not a KeyValuePair.\");\n        var valueProperty = type.GetProperty(nameof(KeyValuePair<object, object>.Value));\n        return valueProperty?.GetValue(keyValuePair);\n    }\n\n    /// <summary>\n    /// An enumerator for <see cref=\"ReferenceEnumerable\"/> that enumerates in proper item order.\n    /// </summary>\n    public readonly struct ReferenceEnumerator : IEnumerator<ObjectReference>\n    {\n        private readonly IEnumerator<NodeIndex> indexEnumerator;","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Quantum/References/ReferenceEnumerable.cs#L196-L232","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only pass items obtained from enumerating the dictionary node (they are KeyValuePair<,> instances).","If you have a key directly, build a NodeIndex from it instead of routing through GetKey/key.","Convert DictionaryEntry to KeyValuePair<TKey,TValue> before passing."],"exampleFix":"// before\nvar idx = enumerableReference.key((DictionaryEntry)entry);\n// after\nvar pair = new KeyValuePair<string, object>((string)entry.Key, entry.Value);\nvar idx = enumerableReference.key(pair);","handlingStrategy":"type-guard","validationCode":"var t = item.GetType();\nbool isKvp = t.IsGenericType && t.GetGenericTypeDefinition() == typeof(KeyValuePair<,>);\nif (isKvp) var idx = reference.key(item);","typeGuard":"bool IsKeyValuePair(object o) => o.GetType() is { IsGenericType: true } t && t.GetGenericTypeDefinition() == typeof(KeyValuePair<,>);","tryCatchPattern":"try { var idx = reference.key(item); }\ncatch (ArgumentException) { /* item is not a KeyValuePair<,>; obtain it by enumerating the reference */ }","preventionTips":["Only feed key()/value() with items from enumerating the ReferenceEnumerable","Convert DictionaryEntry to KeyValuePair<,> first","Don't pass raw keys or tuples"],"tags":["argument-validation","keyvaluepair","reflection"],"backgroundTag":"type-mismatch","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}