{"record":{"id":"6fb4b7d376f4fdeb","repo":"dotnet/runtime","slug":"notimplementedexception-arraysegment-for-more-in","errorCode":null,"errorMessage":"NotImplementedException: ArraySegment. For more information see https://aka.ms/dotnet-wasm-jsinterop","messagePattern":"NotImplementedException: ArraySegment\\. For more information see https://aka\\.ms/dotnet-wasm-jsinterop","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/mono/browser/runtime/marshal-to-cs.ts","lineNumber":435,"sourceCode":"                throw new Error(\"NotImplementedException: TypedArray\");\n            } else if (isThenable(value)) {\n                marshal_task_to_cs(arg, value);\n            } else if (value instanceof Span) {\n                throw new Error(\"NotImplementedException: Span\");\n            } else if (js_type == \"object\") {\n                const js_handle = mono_wasm_get_js_handle(value);\n                set_arg_type(arg, MarshalerType.JSObject);\n                if (BuildConfiguration === \"Debug\" && Object.isExtensible(value)) {\n                    value[proxy_debug_symbol] = `JS Object with JSHandle ${js_handle}`;\n                }\n                set_js_handle(arg, js_handle);\n            } else {\n                throw new Error(`JSObject proxy is not supported for ${js_type} ${value}`);\n            }\n        } else {\n            assert_not_disposed(value);\n            if (value instanceof ArraySegment) {\n                throw new Error(\"NotImplementedException: ArraySegment. \" + jsinteropDoc);\n            } else if (value instanceof ManagedError) {\n                set_arg_type(arg, MarshalerType.Exception);\n                set_gc_handle(arg, gc_handle);\n            } else if (value instanceof ManagedObject) {\n                set_arg_type(arg, MarshalerType.Object);\n                set_gc_handle(arg, gc_handle);\n            } else {\n                throw new Error(\"NotImplementedException \" + js_type + \". \" + jsinteropDoc);\n            }\n        }\n    }\n}\n\nexport function marshal_array_to_cs (arg: JSMarshalerArgument, value: Array<any> | TypedArray | undefined | null, element_type?: MarshalerType): void {\n    mono_assert(!!element_type, \"Expected valid element_type parameter\");\n    marshal_array_to_cs_impl(arg, value, element_type);\n}\n","sourceCodeStart":417,"sourceCodeEnd":453,"githubUrl":"https://github.com/dotnet/runtime/blob/60108ba66eb7d1d12f595480091b4ad80a24b172/src/mono/browser/runtime/marshal-to-cs.ts#L417-L453","documentation":"Thrown by marshal_cs_object_to_cs() when an ArraySegment value (that has a C# gc_handle, i.e., it was created by C# and received by JS) is passed back to a C# method whose parameter is 'object'. The runtime supports round-tripping ArraySegments only through the dedicated ArraySegment<T> marshaler (_marshal_array_segment_to_cs), not through the generic object marshaler. If C# declares the parameter as 'object', the marshal_cs_object_to_cs function encounters the ArraySegment and explicitly throws.","triggerScenarios":"Receiving an ArraySegment<T> from a C# JSExport, then passing it to another C# method whose parameter is declared as 'object' instead of ArraySegment<T>. The value has a gc_handle (from setup_managed_proxy in _marshal_array_segment_to_js), so it enters the gc_handle-defined branch at line 434.","commonSituations":"Forwarding a C#-created ArraySegment to a different C# method with a loose 'object' signature. Using a generic object-passing API to relay managed values.","solutions":["Declare the C# receiving parameter as ArraySegment<T> (e.g., ArraySegment<byte>) with the matching element type so the _marshal_array_segment_to_cs marshaler handles it.","If you need to pass it as object, extract the underlying data first: read the ArraySegment's buffer into a Uint8Array and pass that as byte[].","Keep the ArraySegment reference and pass it only to methods with explicit ArraySegment<T> parameters.","Redesign the loosely-typed object API to use specific types."],"exampleFix":"// before (ArraySegment passed to object parameter)\n// C#: [JSExport] public static void Consume(object data) { }\n// JS:  const seg = GetArraySegmentFromCSharp();\n//      Consume(seg); // throws NotImplementedException: ArraySegment\n\n// after (use ArraySegment<byte> parameter)\n// C#: [JSExport] public static void Consume(ArraySegment<byte> data) { }\n// JS:  Consume(seg); // works via _marshal_array_segment_to_cs","handlingStrategy":"type-guard","validationCode":"// Check if value is an ArraySegment before passing to object parameter\nfunction isArraySegment(value: any): boolean {\n  return value !== null && typeof value === 'object' && value.constructor && value.constructor.name === 'ArraySegment';\n}\n\nif (isArraySegment(myValue)) {\n  // use ArraySegment<T> parameter or copy data\n  throw new Error('Use ArraySegment<T> parameter for ArraySegment values, not object');\n}","typeGuard":"function isArraySegmentLike(value: unknown): boolean {\n  return value !== null && typeof value === 'object'\n    && '_pointer' in (value as any)\n    && 'length' in (value as any)\n    && '_viewType' in (value as any)\n    && js_owned_gc_handle_symbol in (value as any);\n}","tryCatchPattern":null,"preventionTips":["Always declare C# parameters as ArraySegment<T> when receiving ArraySegment values.","Never forward C#-created ArraySegments to methods with 'object' parameters.","If a generic API is needed, copy the ArraySegment data to a byte[] first."],"tags":["marshaling","arraysegment","type-mismatch","interop"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}