method that accepts a generic class as argument in c# -


in project have generic class, used store data , pass function:

public class sqlcommandparameter<t> {     public string parametername { get;private set; }     public sqldbtype sqldbtype { get; private set; }     public t sqldbvalue { get; private set; }      public sqlcommandparameter(string parametername, sqldbtype sqldbtype, t sqldbvalue)     {         parametername = parametername;         sqldbtype = sqldbtype;         sqldbvalue = sqldbvalue;     } } 

but when tried pass instances of function, gives error: cannot resolve t. here method declaration:

 public task<datatable> getdataasync(int? id, string commandtextquery, commandtype commandtype,params sqlcommandparameter<t>[] parameters )     { ... }  

since number or values stored procedure different, i'm passing params. how can pass generic class function in proper way? can suggest way without error?

you can make getdataasync() generic method, getdataasync<t>() all parameters limited whatever t happens when call method:

getdataasync(..., new sqlcommandparameter<int>(), new sqlcommandparameter<int>()); 

will work, but

getdataasync(..., new sqlcommandparameter<int>(), new sqlcommandparameter<string>()); 

will not. 1 option derive generic sqlcommandparameter<t> non-generic sqlcommandparameter , pass in non-generic non-generic getdataasync():

public abstract class sqlcommandparameter {     public string parametername { get; private set; }     public sqldbtype sqldbtype { get; private set; }      protected sqlcommandparameters(string parametername, sqldbtype sqldbtype)     {         parametername = parametername;         sqldbtype = sqldbtype;     } }  public class sqlcommandparameter<t>: sqlcommandparameter {     public t sqldbvalue { get; private set; }      public sqlcommandparameter(string parametername, sqldbtype sqldbtype, t sqldbvalue): base(parametername, sqldbtype)     {         sqldbvalue = sqldbvalue;     } } 

with above, call below work:

getdataasync(..., new sqlcommandparameter<int>(), new sqlcommandparameter<string>()); 

if signature of method is

public task<datatable> getdataasync(     int? id,     string commandtextquery,     commandtype commandtype,     params sqlcommandparameter[] parameters )  { ... } 

another solution create container generic parameters , pass in container method without params because passing in 1 collection.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -