c# - Generics and types values in my method show error -


i'm trying write generic method, , code below gives errors.

cannot convert type 't2' 't1'

't1' not contain definition 'action' , no extension method 'action' accepting first argument of type 't1' found (are missing using directive or assembly reference?)

private list<t2> fillchildcontrolonsave<t1, t2>(         ref t1 objentity, ref list<t1> _entityparent, ref list<t2> _entitydetail) {     foreach (t2 c in _entitydetail)     {         if (c.action == xerp.entity.actionmode.add)                         objentity.plnbomdetails.add(c);          var tmp = objentity.plnbomdetails                      .where(p => p.bomdetailrecordid == c.bomdetailrecordid &&                                   p.bomid == c.bomid &&                                   p.isdeleted == false)                      .firstordefault();          if (tmp != null)            if (c.action == entity.actionmode.delete)                            objcontroller.context.plnbomdetails.deleteobject(tmp);                 }      return _entitydetail;  } 

if replace t1 , t2 plnbommaster,plnbomdetail above syntax works fine. how solve generic method problem?

if want restrict t1 , t2 classes or interfaces, need use generic constraints, this:

private list<t2> fillchildcontrolonsave<t1, t2>(ref t1 objentity,                                                  ref list<t1> _entityparent,                                                  ref list<t2> _entitydetail)     t1 : pinbommaster     t2 : pinbomdetail {     ... } 

of course pinbommaster , pinbomdetail can replaced appropriate interface, such this:

public interface imaster<tdetail>     tdetail : idetail {     list<tdetail> details { get; } }  public interface idetail {     int recordid { get; }     int bomid { get; }     bool isdeleted { get; }     entity.actionmode action { get; set; } }  public class pinbommaster : imaster<pinbomdetail> {     ... }  public class pinbomdetail : idetail {     ... }  private list<t2> fillchildcontrolonsave<t1, t2>(ref t1 objentity,                                                  ref list<t1> _entityparent,                                                  ref list<t2> _entitydetail)     t1 : imaster<t2>     t2 : idetail {     ... } 

note: if entities created code generation tool, you'll have use partial classes apply interface implementations.

of course, can't use objcontroller.context.plnbomdetails.add(c). you'll have replace generic code, this:

// dbcontext objcontroller.context.set<t2>().add(c);  // objectcontext objcontroller.context.createobjectset<t2>().addobject(c); 

of course, can write own method this. example, idetail / imaster interfaces have addtocontext(...) method takes context , inserts appropriate collection. in fillchildcontrolonsave call c.addtocontext(objconctroller.context);.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -