c# - How can I call a generic method on an injected class without informing the parent class of the generic type? -



c# - How can I call a generic method on an injected class without informing the parent class of the generic type? -

below implementation of dependency injected class.

public class csvdataprovider : icsvdataprovider { readonly icsvreaderfactory _factory; public csvdataprovider(icsvreaderfactory factory) { _factory = factory; } public ienumerable<trecord> getdata<trecord>(string filepath) trecord : class { var reader = _factory.createcsvreader(filepath); homecoming reader.getrecords<trecord>(); } }

the reader created factory read lines in csv file , convert each line instance of trecord. not own reader code , can not alter getrecords<trecord>() method.

this stuck:

public class csvdatamigrationcontroller { readonly icsvdataprovider _provider; readonly icsvdatamigration _migration; public csvdatamigrationcontroller(icsvdataprovider provider, icsvdatamigration migration) { _provider = provider; _migration = migration; } public void processfile(string path) { var records = _provider.getdata<i_dont_want_to_explicitly_say>(path); //<-- help! _migration.migrate(records); } }

the intent inject info provider , migration procedure class csvdatamigrationcontroller. controller phone call info provider info , pass info migration class.

i don't want csvdatamigrationcontroller know type of info involved. i don't want csvdataprovider know migration. i don't want csvdatamigration know info came from.

any advice on how can accomplish this?

note: did not include csvdatamigration class because didn't think useful in explanation, include if needed.

well, since can't alter getrecords, perhaps straightforward way inquire migration interface record type, , utilize reflection phone call generic getdata method runtime-obtained type. like:

public void processfile(string path) { type recordtype = _migration.inputrecordtype; var getdatamethod = _provider.gettype() .getmethod("getdata") .makegenericmethod(recordtype); var records = getdatamethod.invoke(_provider, new object[] { path }); _migration.migrate(records); }

c# .net generics interface dependency-injection

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -