c# - How to Convert a generic type list of data to another generic type list of data -


i trying write generic base service class after receiving fist generic list of data actual type of db model entity need conversion new generic view model type of data.

i have tried list.convertall() getting build error convertall() method.

i tried list.cast<tvm>().tolist() solve build error getting run time error.

here code snips of classes , interfaces. or suggestion appreciated.

entity class

public abstract class entity {     [key]     [index("ix_id", 1, isunique = true)]     public string id { get; set; }      [datatype(datatype.datetime)]     public datetime created { get; set; }      public string createdby { get; set; }      [datatype(datatype.datetime)]     public datetime modified { get; set; }      public string modifiedby { get; set; }      [defaultvalue(true)]     public bool active { get; set; } } 

baseviewmodel class

public abstract class baseviewmodel<t> t: entity {              protected baseviewmodel() { }      protected baseviewmodel(t model)     {         id = model.id;         created = model.created;         createdby = model.createdby;         modified = model.modified;         modifiedby = model.modifiedby;         active = model.active;     }      public string id { get; set; }     public datetime created { get; set; }     public string createdby { get; set; }     public datetime modified { get; set; }     public string modifiedby { get; set; }     public bool active { get; set; } } 

ibaseservice interface

public interface ibaseservice<t, tvm> t : entity tvm : baseviewmodel<t> {     list<tvm> getall(); } 

baseservice class

public abstract class baseservice<tentity, tvm> : ibaseservice<tentity, tvm> tentity: entity tvm : baseviewmodel<tentity> {     protected  ibaserepository<tentity> repository;      protected baseservice(ibaserepository<tentity> repository)     {         repository = repository;     }      public virtual  list<tvm> getall()     {         list<tvm> entities;         try         {             list<tentity> list = repository.getall().tolist();             entities =  list.cast<tvm>().tolist(); //runtime error             entities = list.convertall(x => new tvm(x)); //build error             entities = list.convertall(new converter<tentity, tvm>(tentity)); //build error         }         catch (exception exception)         {                             throw new exception(exception.message);         }          return entities;     }  } 

to create instance of generic type, need new()-constraint on it. however, not allow pass parameters it. try either

  1. using activator create instance, this

    entities = list.convertall(x => (tvm)activator.createinstance(typeof(tvm), x));

or

  1. add new()-constraint tvm in baseservice class signature, , add method on classes pass tvm current constructor does, in method, , call after creating new object.

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -