c# - How to pass generic in an interface (nested generics) -
i don't know whether i'm approaching right angle or not, either way can't find syntax works.
i want pass 3 generic types method - there i'll use reflection create objects when need them. object i'm passing generic.
it sort of sounds nested generics.
let's create interface:
public interface iagent<trequest, tclient, tresponse> { }
i have class implements iagent:
public class myclass : iagent<?>
then want call method this:
public method mymethod<t>(t obj) t : iagent<?> { // somethings here }
update
here's @ class level.
public sealed class t3agent : appagent<t3requestadapter, t3webclient, t3responseadapter> { } public abstract class appagent<trequest, tclient, tresponse> trequest : iagentrequestadapter tclient : customwebclient tresponse : iagentresponseadapter { public appresponse submit(iappform appform, servicelog log) { } } public sealed class appmanager { public appresponse submit() { var t3 = new t3agent(); var t3result = t3.submit(form, log); return t3result; } }
in final method, instantiate new class (t3agent), tightly coupled t3. want able instantiate appagent once , pass required generic types through method rather class, can reuse class.
i think have 2 options:
- if have work generic class t agent<t> have know type anyway have put in generic declaration (see typeneeded() method below)
if not necessary, split interface in not generic , in generic interface , let iagent<t> extend iagent (iagent<t>: iagent). can create method without know type iagent<t> (see method dosomething())
public interface iagent { void action( ); int calculate( ); } public interface iagent< t > : iagent { void set( t value ); } public class myclass { public void dosomething< t >( t agent ) t : iagent { //... } public void typeneeded< t, v >( t agent ) t : iagent<v> { } }
Comments
Post a Comment