{"record":{"id":"18f08eecf5f1f12f","repo":"stride3d/stride","slug":"objectvalue-type-does-not-match-objecttype","errorCode":null,"errorMessage":"objectValue type does not match objectType","messagePattern":"objectValue type does not match objectType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Quantum/References/Reference.cs","lineNumber":16,"sourceCode":"// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)\n// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.\n\nusing System.Collections;\nusing Stride.Core.Reflection;\n\nnamespace Stride.Core.Quantum.References;\n\ninternal static class Reference\n{\n    private static readonly ThreadLocal<int> CreatingReference = new();\n\n    [Obsolete(\"This method will be rewritten to handle separately the different types of references\")]\n    internal static IReference? CreateReference(object? objectValue, Type objectType, NodeIndex index, bool isMember)\n    {\n        if (objectValue != null && !objectType.IsInstanceOfType(objectValue)) throw new ArgumentException(@\"objectValue type does not match objectType\", nameof(objectValue));\n\n        if (!CreatingReference.IsValueCreated)\n            CreatingReference.Value = 0;\n\n        ++CreatingReference.Value;\n\n        IReference? reference;\n        var isCollection = HasCollectionReference(objectValue?.GetType() ?? objectType);\n        if (isMember)\n        {\n            reference = new ObjectReference(objectValue, objectType, index);\n        }\n        else\n        {\n            if (objectValue != null && isCollection && index.IsEmpty)\n            {\n                reference = new ReferenceEnumerable((IEnumerable)objectValue, objectType);\n            }","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Quantum/References/Reference.cs#L1-L34","documentation":"Reference.CreateReference is the single factory for building IReference instances in Stride.Core.Quantum. Before creating the reference it validates that the runtime object actually is an instance of the declared node type; if objectValue is non-null and not assignable to objectType, the graph would hold a reference whose content does not match its declared type, so it throws ArgumentException immediately.","triggerScenarios":"Calling Reference.CreateReference(objectValue, objectType, index, isMember) with a non-null objectValue whose concrete type is not assignable to objectType — e.g. CreateReference(\"someString\", typeof(int), index, false) or passing a derived object where an unrelated base was declared.","commonSituations":"Custom NodeFactory/NodeBuilder code that mismatches reflection metadata with actual values; generic code that passes Type objects captured from a different member; refactoring a property's type without updating the code that declares the node type; obsolete-API usage paths that construct references by hand.","solutions":["Verify the objectValue you pass is an instance of objectType before calling (objectValue is null or objectType.IsInstanceOfType(objectValue)).","Make sure the Type comes from the same reflection source (descriptor/member) as the value — prefer nodeContainer/factory helpers over hand-built references.","If you truly have a mismatch, use the correct declared type for the value, or cast/convert the value to the declared type first.","Note the method is marked Obsolete ('will be rewritten to handle separately the different types of references') — migrate to the current reference-creation API in your Stride version."],"exampleFix":"// before\nvar reference = Reference.CreateReference(order, typeof(Customer), nodeIndex, false);\n// after\nvar reference = Reference.CreateReference(order, order.GetType(), nodeIndex, false);","handlingStrategy":"validation","validationCode":"if (objectValue is null || objectType.IsInstanceOfType(objectValue))\n    var reference = Reference.CreateReference(objectValue, objectType, nodeIndex, isMember);","typeGuard":"bool IsCompatible(object? value, Type declaredType) => value is null || declaredType.IsInstanceOfType(value);","tryCatchPattern":"try { var r = Reference.CreateReference(value, type, index, isMember); }\ncatch (ArgumentException ex) when (ex.ParamName == \"objectValue\") { /* fix declared type or value source */ }","preventionTips":["Derive both value and Type from the same reflection descriptor/member","Prefer container/factory APIs over hand-constructing references","Avoid the Obsolete CreateReference; use the current Stride reference-creation API"],"tags":["argument-validation","type-mismatch","quantum"],"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"}