{"record":{"id":"427ba31c1b1fdfea","repo":"stride3d/stride","slug":"valuetype-is-not-one-of-the-expected-types","errorCode":null,"errorMessage":"ValueType is not one of the expected types.","messagePattern":"ValueType is not one of the expected types\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Diagnostics/ProfilingCustomValue.cs","lineNumber":32,"sourceCode":"    [FieldOffset(0)]\n    public float FloatValue;\n\n    [FieldOffset(0)]\n    public long LongValue;\n\n    [FieldOffset(0)]\n    public double DoubleValue;\n\n    [FieldOffset(8)]\n    public Type ValueType;\n\n    public readonly object ToObject()\n    {\n        if (ValueType == typeof(int)) return IntValue;\n        else if (ValueType == typeof(float)) return FloatValue;\n        else if (ValueType == typeof(long)) return LongValue;\n        else if (ValueType == typeof(double)) return DoubleValue;\n        else throw new InvalidOperationException($\"{nameof(ValueType)} is not one of the expected types.\");\n    }\n\n    public static implicit operator ProfilingCustomValue(int value)\n    {\n        return new ProfilingCustomValue { IntValue = value, ValueType = typeof(int) };\n    }\n\n    public static implicit operator ProfilingCustomValue(float value)\n    {\n        return new ProfilingCustomValue { FloatValue = value, ValueType = typeof(float) };\n    }\n\n    public static implicit operator ProfilingCustomValue(long value)\n    {\n        return new ProfilingCustomValue { LongValue = value, ValueType = typeof(long) };\n    }\n\n    public static implicit operator ProfilingCustomValue(double value)","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Diagnostics/ProfilingCustomValue.cs#L14-L50","documentation":"ProfilingCustomValue.ToObject() converts the stored union (IntValue/FloatValue/LongValue/DoubleValue) back to object based on ValueType. If ValueType holds anything but those four types, the internal invariant is broken and it throws InvalidOperationException naming ValueType.","triggerScenarios":"Constructing a ProfilingCustomValue with object initializer/struct field assignment setting ValueType to e.g. typeof(string), typeof(bool) or leaving it null while setting a value field; misuse of the struct outside its implicit-operator API.","commonSituations":"Custom profiling code that bypasses the implicit conversions from int/float/long/double and hand-rolls the struct; serialization round-trips that restore ValueType incorrectly; adding support for a new type without updating ToObject.","solutions":["Set the value only through the implicit operators (int/float/long/double) so ValueType is always valid","If you need another type, extend ValueType checks in ToObject and add a corresponding implicit operator","Default-initialize ValueType to the matching type when constructing manually"],"exampleFix":"// before\nnew ProfilingCustomValue { ValueType = typeof(string), /* no string field */ }\n// after\nProfilingCustomValue v = 42; // sets ValueType = typeof(int), IntValue = 42","handlingStrategy":"validation","validationCode":"var supported = new[]{ typeof(int), typeof(float), typeof(long), typeof(double) };\nobject value = supported.Contains(v.ValueType) ? v.ToObject() : throw new InvalidOperationException();","typeGuard":"bool isKnownCustomValue(ProfilingCustomValue v) =>\n    v.ValueType == typeof(int) || v.ValueType == typeof(float) || v.ValueType == typeof(long) || v.ValueType == typeof(double);","tryCatchPattern":"try { obj = customValue.ToObject(); }\ncatch (InvalidOperationException) { obj = null; /* log invariant breach */ }","preventionTips":["Only build ProfilingCustomValue via its implicit operators","Never hand-assign ValueType in initializers","Update both ValueType checks and operators together when extending types"],"tags":["profiling","invalidoperationexception","union-type","invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}