How to call a Generic method with dynamic properties in C# -


i have few methods have similar signature , trying convert them 1 generic 1 without use of interfaces.

 public list<multiselectdropdown> convertlistofjobstatustodropdownlistclickable(list<jobstatus> js) {         var list = new list<multiselectdropdown>();         if (js != null && js.count >= 1) {             list = js.select(item => new multiselectdropdown { name = item.statusvalue, value = item.id.tostring() }).tolist();         }         return list;     }       public list<multiselectdropdown> convertlistofcuserstodropdownlistclickable(list<cuser> users) {         var list = new list<multiselectdropdown>();         if (users != null && users.count >= 1) {             list = users.select(item => new multiselectdropdown { name = item.user_name, value = item.id.tostring() }).tolist();         }         return list;     } 

this do; pass in list 2 properties.

list<multiselectdropdown> ddlforclientusers = converttomultiselectdropdownlist(listofclientsforuser, n => n.client_id, v => v.client);  list<multiselectdropdown> ddlforjobstatus = converttomultiselectdropdownlist(listofjobstatus, n => n.id, v => v.jobname); 

this method have tried not sure how item.propname , item.propvalue work.

i "cannot resolve" propname , propvalue in method below

is possible?

 public list<multiselectdropdown> converttomultiselectdropdownlist<t, tpropertyname, tpropertyvalue>(list<t> listoft, func<t, tpropertyname> propname, func<t, tpropertyvalue> propvalue) { var list = new list<multiselectdropdown>();         if (listoft != null && listoft.count >= 1) {             list = listoft.select(item => new multiselectdropdown { name = item.propname, value = item.propvalue }).tolist();         }         return list;     }  public class multiselectdropdown {     public string name { get; set; }     public string value { get; set; }     public bool ischecked { get; set; } } 

because properties of multiselectdropdown strings, functions should return well. , invoke functions, have write them propname(item) instead of item.propname - property syntax, , indicated didn't want use interfaces.

public list<multiselectdropdown> converttomultiselectdropdownlist<t>(list<t> listoft, func<t, string> propname, func<t, string> propvalue) {     var list = new list<multiselectdropdown>();     if (listoft != null && listoft.count >= 1) {         list = listoft.select(item => new multiselectdropdown { name = propname(item), value = propvalue(item) }).tolist();     }     return list; } 

Comments