restsharp/RestSharp · error · InvalidOperationException
The type must contain a public, parameterless constructor.
Error message
The type must contain a public, parameterless constructor.
What it means
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.
Source
Thrown at src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs:45
using var stringReader = new StringReader(response.Content);
using var csvReader = new CsvReader(stringReader, configuration);
var @interface = typeof(T).GetInterface("IEnumerable`1");
if (@interface == null) {
csvReader.Read();
return csvReader.GetRecord<T>();
}
var itemType = @interface.GenericTypeArguments[0];
T result;
try {
result = Activator.CreateInstance<T>();
}
catch (MissingMethodException) {
throw new InvalidOperationException(message: "The type must contain a public, parameterless constructor.");
}
var method = typeof(T).GetMethod(name: "Add");
if (method == null) {
throw new InvalidOperationException(
message: "If the type implements IEnumerable<T>, then it must contain a public \"Add(T)\" method."
);
}
foreach (var record in csvReader.GetRecords(itemType)) {
method.Invoke(result, [record]);
}
return result;
}
catch (Exception exception) {
throw new DeserializationException(response, exception);View on GitHub (pinned to 6a50821692)
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.
Example fix
// before var data = await client.GetAsync<IEnumerable<Foo>>(request); // after var data = await client.GetAsync<List<Foo>>(request);
Defensive patterns
Strategy: validation
Validate before calling
var hasParameterlessCtor = typeof(T).GetConstructor(Type.EmptyTypes) != null;
if (!hasParameterlessCtor) throw new InvalidOperationException($"{typeof(T)} needs a public parameterless ctor for CSV deserialization"); Type guard
static bool IsCsvCollectionWithCtor<T>() => typeof(T).GetInterface("IEnumerable`1") != null && typeof(T).GetConstructor(Type.EmptyTypes) != null; Try / catch
try { var data = client.GetAsync<List<T>>(req); } catch (DeserializationException ex) when (ex.InnerException?.Message.Contains("parameterless constructor") == true) { /* use List<T> instead */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- If the type implements IEnumerable<T>, then it must contain
- Response content is null
- Response content is null
- Class cannot have two properties marked with SerializeAs(Con
- Couldn't parse the value of '{value}' into the '{prop.Name}'
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/334ee19c3d40a870.
Report an issue: GitHub.