c# - Generic method in class -


my code:

public class myclass {     private wdmentities _context;      public list<t> gettable<t>()     {         list<t> res = new list<t>();         _context = new dbentities();          if (typeof(t) == typeof(tables))             res = _context.tables.tolist();//error cannot implicitly convert type         if (typeof(t) == typeof(columns))             res = _context.columns.tolist();//error cannot implicitly convert type          return res;     }  } 

i have tables , columns types entitymodel

but got compile error:

 cannot implicitly convert type 

how must change code make work?

you can use cast method here.

if (typeof(t) == typeof(tables))     res = _context.tables.cast<t>().tolist(); if (typeof(t) == typeof(columns))     res = _context.columns.cast<t>().tolist(); 

however not generic method. have change code when need return list of type. should find way items context using generic method. if that's not possible suggest ditching generic method , writing 2 seperate methods these types.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -