c# - Cannot Convert Type System.Collection.Generic.List<T> -


i trying use generics reduce codebase, ran situation. can't seem create google query expresses trying do.

in essence passing in generic make list<t> , pass list<t> function requires list<specificclass>

my jsonuser class

    public class jsonuser     {         public string documentid { get; set; }         [jsonproperty(propertyname = "username", required = required.always)]         public string username { get; set; }         [jsonproperty(propertyname = "firstname", required = required.allownull)]         public string firstname { get; set; }         [jsonproperty(propertyname = "lastname", required = required.allownull)]         public string lastname { get; set; }     } 

assume there class jsoncompany company fields.

main code:

static void collectuserdata() {      boolean j = getjson<jsonuser>();      boolean k = getjson<jsoncompany>();      ... }  static boolean getjson<t>() {    ...    // json in list    list<t> ojson = callrest<t>();     // depending on type passed in, call different    // process function expects list<jsonuser> or    // list<jsoncompany> parameter     if (typeof(t) == typeof(jsonuser))    {        boolean result = processuser(ojson);        ...    }    else if (typeof(t) == typeof(jsoncompany))    {        boolean result = processcompany(ojson);        ...    } ... }  public boolean processuser(list<jsonuser> jsonlist) {     ... }  public boolean processcompany(list<jsoncompany> jsonlist) {     ... } 

everything until call processuser(ojson);

it says there no method accept generic. when try cast it, says

cannot covert type system.collection.generic.list<t> system.collection.generic.list<jsonuser>

hopefully clear.

if processuser et al not require list , can work ienumerable<t> can simplify bit:

public boolean processuser(ienumerable<jsonuser> jsonlist) {     ... }  public boolean processcompany(ienumerable<jsoncompany> jsonlist) {     ... } 

then call with:

boolean result = processuser(ojson.cast<jsonuser>()); 

otherwise could create new list:

boolean result = processuser(ojson.cast<jsonuser>().tolist()); 

which may fine if you're iteerating/modifying objects in list , not list itself. (adding/removing/sorting/etc.)


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -