dotnet/runtime · error · Error

NotImplementedException: ArraySegment. ${jsinteropDoc}

Error message

NotImplementedException: ArraySegment. ${jsinteropDoc}

What it means

In the gcHandle-present branch of marshalCsObjectToCs, if the value is an ArraySegment instance the marshaler throws NotImplementedException. ArraySegment is only supported as a round-trip via the explicit ArraySegment marshaler (_marshalArraySegmentToCs), which requires the value to come from C# and be marshaled on a signature typed for ArraySegment - not through the generic object converter. The message appends jsinteropDoc (https://aka.ms/dotnet-wasm-jsinterop).

Source

Thrown at src/native/libs/System.Runtime.InteropServices.JavaScript.Native/interop/marshal-to-cs.ts:406

                throw new Error("NotImplementedException: TypedArray");
            } else if (isThenable(value)) {
                marshalTaskToCs(arg, value);
            } else if (value instanceof Span) {
                throw new Error("NotImplementedException: Span");
            } else if (jsType == "object") {
                const jsHandle = getJsHandleFromJSObject(value);
                setArgType(arg, MarshalerType.JSObject);
                if (BuildConfiguration === "Debug" && Object.isExtensible(value)) {
                    value[proxyDebugSymbol] = `JS Object with JSHandle ${jsHandle}`;
                }
                setJsHandle(arg, jsHandle);
            } else {
                throw new Error(`JSObject proxy is not supported for ${jsType} ${value}`);
            }
        } else {
            assertNotDisposed(value);
            if (value instanceof ArraySegment) {
                throw new Error("NotImplementedException: ArraySegment. " + jsinteropDoc);
            } else if (value instanceof ManagedError) {
                setArgType(arg, MarshalerType.Exception);
                setGcHandle(arg, gcHandle);
            } else if (value instanceof ManagedObject) {
                setArgType(arg, MarshalerType.Object);
                setGcHandle(arg, gcHandle);
            } else {
                throw new Error("NotImplementedException " + jsType + ". " + jsinteropDoc);
            }
        }
    }
}

export function marshalArrayToCs(arg: JSMarshalerArgument, value: Array<any> | TypedArray | undefined | null, elementType?: MarshalerType): void {
    dotnetAssert.check(!!elementType, "Expected valid elementType parameter");
    marshalArrayToCsImpl(arg, value, elementType);
}

View on GitHub (pinned to 60108ba66e)

Solutions

  1. Type the C# parameter/return as ArraySegment<T> (with the right T: byte/int/double/float) so the value is routed through _marshalArraySegmentToCs.
  2. Do not pass ArraySegment instances through object-typed interop boundaries.
  3. If only the bytes are needed, extract a Span/byte[] view from the ArraySegment before crossing the object boundary.

Example fix

// before - C#
[JSImport("recv", "m")]
static partial void Receive([MarshalAs(typeof(object))] object seg);

// after
[JSImport("recv", "m")]
static partial void Receive(ArraySegment<byte> seg);
Defensive patterns

Strategy: type-guard

Validate before calling

// Don't forward an ArraySegment through an object-typed boundary; copy out bytes.
import { ArraySegment } from "./marshaled-types";

function arraySegmentToBytes(seg: ArraySegment): Uint8Array | null {
  if (!(seg instanceof ArraySegment)) return null;
  return seg.slice() as Uint8Array;
}

Type guard

function isArraySegment(v: unknown): boolean {
  return v instanceof ArraySegment;
}

Try / catch

null

Prevention

When it happens

Trigger: An ArraySegment created by C# and handed to JS is later passed back through a parameter/return typed as object or JSObject (gcHandle present but the marshaler is the generic object one), hitting the explicit ArraySegment rejection.

Common situations: Storing an ArraySegment received from one interop call and forwarding it into another call whose parameter is typed object instead of ArraySegment<byte> etc.

Related errors


AI-assisted analysis of dotnet/runtime@60108ba66e (2026-08-10). Data as JSON: /api/errors/a7aabea2cc7b7c5d. Report an issue: GitHub.