{"record":{"id":"1e2b0eec15c0136d","repo":"dotnet/machinelearning","slug":"a-primitivedataviewtype-cannot-have-a-disposable-r","errorCode":null,"errorMessage":"A PrimitiveDataViewType cannot have a disposable RawType","messagePattern":"A PrimitiveDataViewType cannot have a disposable RawType","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.DataView/DataViewType.cs","lineNumber":81,"sourceCode":"    public abstract class StructuredDataViewType : DataViewType\n    {\n        protected StructuredDataViewType(Type rawType)\n            : base(rawType)\n        {\n        }\n    }\n\n    /// <summary>\n    /// The abstract base class for all primitive types. Values of these types can be freely copied\n    /// without concern for ownership, mutation, or disposing.\n    /// </summary>\n    public abstract class PrimitiveDataViewType : DataViewType\n    {\n        protected PrimitiveDataViewType(Type rawType)\n            : base(rawType)\n        {\n            if (typeof(IDisposable).GetTypeInfo().IsAssignableFrom(RawType.GetTypeInfo()))\n                throw new ArgumentException(\"A \" + nameof(PrimitiveDataViewType) + \" cannot have a disposable \" + nameof(RawType), nameof(rawType));\n        }\n    }\n\n    /// <summary>\n    /// The standard text type. This has representation type of <see cref=\"ReadOnlyMemory{T}\"/> with type parameter <see cref=\"char\"/>.\n    /// Note this can have only one possible value, accessible by the singleton static property <see cref=\"Instance\"/>.\n    /// </summary>\n    public sealed class TextDataViewType : PrimitiveDataViewType\n    {\n        private static volatile TextDataViewType _instance;\n        /// <summary>\n        /// The singleton instance of this type.\n        /// </summary>\n        public static TextDataViewType Instance\n        {\n            get\n            {\n                return _instance ??","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.DataView/DataViewType.cs#L63-L99","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use a non-disposable representation: plain structs (int, float), ReadOnlyMemory<T>, or a POCO without IDisposable.","If resources are needed, make the type a StructuredDataViewType instead of PrimitiveDataViewType.","Strip the IDisposable implementation from the raw type (dispose externally, not via the value itself)."],"exampleFix":"// before\nclass MyType : PrimitiveDataViewType { public MyType() : base(typeof(ResourceHandle)) { } } // ResourceHandle : IDisposable\n// after\nclass MyType : PrimitiveDataViewType { public MyType() : base(typeof(ReadOnlyMemory<byte>)) { } }","handlingStrategy":"type-guard","validationCode":"if (typeof(IDisposable).IsAssignableFrom(rawType)) throw new ArgumentException(\"Primitive types cannot be disposable\");","typeGuard":"bool IsDisposableSafe(Type t) => t != null && !typeof(IDisposable).IsAssignableFrom(t);","tryCatchPattern":"try { new MyPrimitiveType(); } catch (ArgumentException ex) when (ex.Message.Contains(\"disposable\")) { /* switch to StructuredDataViewType */ }","preventionTips":["Design primitive representations as plain structs or ReadOnlyMemory<T>","Use StructuredDataViewType for resource-holding representations","Grep custom types for IDisposable before registering them"],"tags":["csharp","idisposable","custom-type","ml-net"],"backgroundTag":"invalid-argument-value","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}