dotnet/machinelearning · error · ArgumentException

A PrimitiveDataViewType cannot have a disposable RawType

Error message

A PrimitiveDataViewType cannot have a disposable RawType

What it means

PrimitiveDataViewType's constructor throws ArgumentException("A PrimitiveDataViewType cannot have a disposable RawType") when the raw CLR type implements IDisposable. Primitive types are copied by value throughout the data view pipeline and are never disposed; a disposable representation would leak or break the copying contract, so it is rejected.

Source

Thrown at src/Microsoft.ML.DataView/DataViewType.cs:81

    public abstract class StructuredDataViewType : DataViewType
    {
        protected StructuredDataViewType(Type rawType)
            : base(rawType)
        {
        }
    }

    /// <summary>
    /// The abstract base class for all primitive types. Values of these types can be freely copied
    /// without concern for ownership, mutation, or disposing.
    /// </summary>
    public abstract class PrimitiveDataViewType : DataViewType
    {
        protected PrimitiveDataViewType(Type rawType)
            : base(rawType)
        {
            if (typeof(IDisposable).GetTypeInfo().IsAssignableFrom(RawType.GetTypeInfo()))
                throw new ArgumentException("A " + nameof(PrimitiveDataViewType) + " cannot have a disposable " + nameof(RawType), nameof(rawType));
        }
    }

    /// <summary>
    /// The standard text type. This has representation type of <see cref="ReadOnlyMemory{T}"/> with type parameter <see cref="char"/>.
    /// Note this can have only one possible value, accessible by the singleton static property <see cref="Instance"/>.
    /// </summary>
    public sealed class TextDataViewType : PrimitiveDataViewType
    {
        private static volatile TextDataViewType _instance;
        /// <summary>
        /// The singleton instance of this type.
        /// </summary>
        public static TextDataViewType Instance
        {
            get
            {
                return _instance ??

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Use a non-disposable representation: plain structs (int, float), ReadOnlyMemory<T>, or a POCO without IDisposable.
  2. If resources are needed, make the type a StructuredDataViewType instead of PrimitiveDataViewType.
  3. Strip the IDisposable implementation from the raw type (dispose externally, not via the value itself).

Example fix

// before
class MyType : PrimitiveDataViewType { public MyType() : base(typeof(ResourceHandle)) { } } // ResourceHandle : IDisposable
// after
class MyType : PrimitiveDataViewType { public MyType() : base(typeof(ReadOnlyMemory<byte>)) { } }
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof(IDisposable).IsAssignableFrom(rawType)) throw new ArgumentException("Primitive types cannot be disposable");

Type guard

bool IsDisposableSafe(Type t) => t != null && !typeof(IDisposable).IsAssignableFrom(t);

Try / catch

try { new MyPrimitiveType(); } catch (ArgumentException ex) when (ex.Message.Contains("disposable")) { /* switch to StructuredDataViewType */ }

Prevention

When it happens

Trigger: Defining a custom PrimitiveDataViewType subclass whose raw type is (or implements) IDisposable, e.g. a struct wrapping a Stream or a class implementing IDisposable; passing such a Type to the base constructor.

Common situations: Custom type extension authoring where the representation type was changed to a resource-holding class; migrating an existing StructuredDataViewType-style type into a primitive one.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/1e2b0eec15c0136d. Report an issue: GitHub.