{"id":4425,"date":"2014-03-30T10:54:46","date_gmt":"2014-03-30T10:54:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-call-a-generic-method-on-an-injected-class-without-informing-the-parent-class-of-the-generic-type-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:54:46","modified_gmt":"2014-03-30T10:54:46","slug":"how-can-i-call-a-generic-method-on-an-injected-class-without-informing-the-parent-class-of-the-generic-type-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-call-a-generic-method-on-an-injected-class-without-informing-the-parent-class-of-the-generic-type-collection-of-common-programming-errors\/","title":{"rendered":"How can I call a generic method on an injected class without informing the parent class of the generic type?-Collection of common programming errors"},"content":{"rendered":"<p>Below is the implementation of a dependency that is injected into another class.<\/p>\n<pre><code>public class CsvDataProvider : ICsvDataProvider\n{\n    readonly ICsvReaderFactory _factory;\n\n    public CsvDataProvider(ICsvReaderFactory factory)\n    {\n        _factory = factory;\n    }\n\n    public IEnumerable GetData(string filepath) where TRecord : class\n    {\n        var reader = _factory.CreateCsvReader(filepath);\n\n        return reader.GetRecords();\n    }\n}\n<\/code><\/pre>\n<p>The <code>reader<\/code> that is created by the <code>factory<\/code> will read all lines in a CSV file and convert each line to an instance of <code>TRecord<\/code>. I do not own the <code>reader<\/code> code and can not change the <code>GetRecords()<\/code> method.<\/p>\n<p>This is where I am stuck:<\/p>\n<pre><code>public class CsvDataMigrationController\n{\n    readonly ICsvDataProvider _provider;\n    readonly ICsvDataMigration _migration;\n\n    public CsvDataMigrationController(ICsvDataProvider provider, ICsvDataMigration migration)\n    {\n        _provider = provider;\n        _migration = migration;\n    }\n\n    public void ProcessFile(string path)\n    {\n        var records = _provider.GetData(path); \/\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below is the implementation of a dependency that is injected into another class. public class CsvDataProvider : ICsvDataProvider { readonly ICsvReaderFactory _factory; public CsvDataProvider(ICsvReaderFactory factory) { _factory = factory; } public IEnumerable GetData(string filepath) where TRecord : class { var reader = _factory.CreateCsvReader(filepath); return reader.GetRecords(); } } The reader that is created by the factory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4425","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4425"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4425\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}