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

  1. Deserialize into a concrete collection type that has a public parameterless constructor, such as List<T>.
  2. Avoid using interfaces (IEnumerable<T>, ICollection<T>) or read-only collections as the generic target.
  3. 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

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


AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13). Data as JSON: /api/errors/334ee19c3d40a870. Report an issue: GitHub.