{"record":{"id":"44314a8562bb26ea","repo":"restsharp/RestSharp","slug":"if-the-type-implements-ienumerable-t-then-it-mus","errorCode":null,"errorMessage":"If the type implements IEnumerable<T>, then it must contain a public \"Add(T)\" method.","messagePattern":"If the type implements IEnumerable<T>, then it must contain a public \"Add\\(T\\)\" method\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs","lineNumber":51,"sourceCode":"            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);\n        }\n    }\n\n    public string? Serialize(Parameter parameter) => Serialize(parameter.Value);\n\n    public string? Serialize(object? obj) {","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs#L33-L69","documentation":"Thrown by CsvHelperSerializer.Deserialize when the target type T implements IEnumerable<T> but does not expose a public 'Add(T)' method. After creating an instance the deserializer reflects for an Add method to populate each record; interfaces like IEnumerable<T> or arrays have no usable Add. Re-wrapped in DeserializationException at line 62.","triggerScenarios":"Deserializing into an array (Foo[]), a read-only collection, a type that implements IEnumerable<T> purely for querying but not for mutation, or into an interface type that has no Add method.","commonSituations":"Target type is an array T[]; target is IReadOnlyList<T>; target is a custom type implementing IEnumerable<T> without an Add method.","solutions":["Deserialize into List<T> (or another concrete collection with a public Add(T) method).","If the type must be a custom collection, ensure it exposes a public Add(T) method.","Avoid arrays and read-only interfaces as the deserialization target."],"exampleFix":"// before\nvar data = await client.GetAsync<Foo[]>(request);\n\n// after\nvar data = await client.GetAsync<List<Foo>>(request);","handlingStrategy":"validation","validationCode":"var addMethod = typeof(T).GetMethod(\"Add\");\nif (typeof(T).GetInterface(\"IEnumerable`1\") != null && addMethod == null) throw new InvalidOperationException($\"{typeof(T)} needs a public Add(T) method\");","typeGuard":"static bool IsCsvAddable<T>() => typeof(T).GetInterface(\"IEnumerable`1\") == null || typeof(T).GetMethod(\"Add\") != null;","tryCatchPattern":"try { var data = client.GetAsync<List<T>>(req); } catch (DeserializationException ex) when (ex.InnerException?.Message.Contains(\"\\\"Add(T)\\\" method\") == true) { /* switch to List<T> */ }","preventionTips":["Prefer List<T> as the CSV deserialization target.","Avoid arrays and read-only interfaces for CSV deserialization."],"tags":["csv","deserialization","reflection","collection","csvhelper"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}