{"record":{"id":"334ee19c3d40a870","repo":"restsharp/RestSharp","slug":"the-type-must-contain-a-public-parameterless-cons","errorCode":null,"errorMessage":"The type must contain a public, parameterless constructor.","messagePattern":"The type must contain a public, parameterless constructor\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs","lineNumber":45,"sourceCode":"\n            using var stringReader = new StringReader(response.Content);\n            using var csvReader    = new CsvReader(stringReader, configuration);\n\n            var @interface = typeof(T).GetInterface(\"IEnumerable`1\");\n\n            if (@interface == null) {\n                csvReader.Read();\n                return csvReader.GetRecord<T>();\n            }\n\n            var itemType = @interface.GenericTypeArguments[0];\n            T   result;\n\n            try {\n                result = Activator.CreateInstance<T>();\n            }\n            catch (MissingMethodException) {\n                throw new InvalidOperationException(message: \"The type must contain a public, parameterless constructor.\");\n            }\n\n            var method = typeof(T).GetMethod(name: \"Add\");\n\n            if (method == null) {\n                throw new InvalidOperationException(\n                    message: \"If the type implements IEnumerable<T>, then it must contain a public \\\"Add(T)\\\" method.\"\n                );\n            }\n\n            foreach (var record in csvReader.GetRecords(itemType)) {\n                method.Invoke(result, [record]);\n            }\n\n            return result;\n        }\n        catch (Exception exception) {\n            throw new DeserializationException(response, exception);","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs#L27-L63","documentation":"Thrown by CsvHelperSerializer.Deserialize when the target type T implements IEnumerable<T> (a collection) but cannot be instantiated via Activator.CreateInstance because it lacks a public parameterless constructor. The deserializer creates an empty collection instance and then adds records into it, so it must be constructible. Re-wrapped in DeserializationException at line 62.","triggerScenarios":"Deserializing into a collection type that has no public parameterless constructor, e.g. a custom collection with only a constructor that takes arguments, a read-only interface like IEnumerable<T> or IReadOnlyList<T>, or a type requiring constructor injection.","commonSituations":"Using IEnumerable<T> as the deserialization target instead of a concrete List<T>; deserializing into an interface; using a custom collection class that only has parameterized constructors.","solutions":["Deserialize into a concrete collection type that has a public parameterless constructor, such as List<T>.","Avoid using interfaces (IEnumerable<T>, ICollection<T>) or read-only collections as the generic target.","If using a custom collection, add an explicit public parameterless constructor to it."],"exampleFix":"// before\nvar data = await client.GetAsync<IEnumerable<Foo>>(request);\n\n// after\nvar data = await client.GetAsync<List<Foo>>(request);","handlingStrategy":"validation","validationCode":"var hasParameterlessCtor = typeof(T).GetConstructor(Type.EmptyTypes) != null;\nif (!hasParameterlessCtor) throw new InvalidOperationException($\"{typeof(T)} needs a public parameterless ctor for CSV deserialization\");","typeGuard":"static bool IsCsvCollectionWithCtor<T>() => typeof(T).GetInterface(\"IEnumerable`1\") != null && typeof(T).GetConstructor(Type.EmptyTypes) != null;","tryCatchPattern":"try { var data = client.GetAsync<List<T>>(req); } catch (DeserializationException ex) when (ex.InnerException?.Message.Contains(\"parameterless constructor\") == true) { /* use List<T> instead */ }","preventionTips":["Always deserialize CSV collections into List<T>, not interfaces or custom collections without a parameterless ctor.","Add a compile-time/code-review check that CSV target types are concrete collections."],"tags":["csv","deserialization","reflection","constructor","csvhelper"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}