c# - Attach Generic method event handler with unknow type -


i need attach handler radlistview column creation, adding datasource control.

 public void genericcolumncreatinghandler<t>(object sender, listviewcolumncreatingeventargs e)     {         e.column.visible = baseentity<int>.membervisibility<t>            (e.column.fieldname, telerikpropertyvisibilityattribute.visibilitytypeenum.basedetails);          e.column.headertext = caricatestolocale(e.column.headertext, "col_" + e.column.headertext);                                  e.column.bestfit();         e.column.autosizemode = listviewbestfitcolumnmode.allcells;     }  

my problem need perform handler attach other generic method:

  private void populaterecord(tipotabellabase tipo)     {         type generic = typeof(commontableservice<>);         type[] typeargs = { tipo.tipo };         var constructed = generic.makegenerictype(typeargs);          var instance = activator.createinstance(constructed);         if (instance == null)             return;          methodinfo getentities = constructed.getmethod("getentitieswithnoparameters");         //getentities = getentities.makegenericmethod(typeargs);                      var result = (ienumerable<baseentity<int>>)getentities.invoke(instance, null);                                                                                            lvrecords.columncreating += base.genericcolumncreatinghandler<baseentity<int>>;         lvrecords.datasource = result;         bestfit(lvrecords);          generic = null;         typeargs = null;         constructed = null;         getentities = null;         instance = null;               } 

the problematic row one:

 lvrecords.columncreating += base.genericcolumncreatinghandler<baseentity<int>> 

because baseentity ef base type entities, not enought baseentity.membervisibility method; method need know exact entity type set visible properties (and, of course, grid column) based on specific custom attribute on that.

question is: how can call base.genericcolumncreatinghandler t tipotabellabase tipo.tipo (type) without knowing type @ design time?

any appreciated! advance.

daniel

please note solution untested.

you have instantiate strongly-typed version of base.genericcolumncreatinghandler<t> @ runtime.

from code, figure know how obtain methodinfo instance given method. need methodinfo base.genericcolumncreatinghandler<t> (let's call genericmethodinfo).

then, can create strongly-typed version of method makegenericmethod:

methodinfo typedmethodinfo = genericmethodinfo.makegenericmethod(new[] {                                  typeof(baseentity<int>)                              }); 

once done, need invoke createdelegate obtain can assign columncreating event, described here or here:

lvrecords.columncreating +=     (listviewcolumncreatingeventhandler)typedmethodinfo.createdelegate(         typeof(listviewcolumncreatingeventhandler), this); 

edit: replaced base this in last code sample. in case inherited method required, has taken care of while retrieving genericmethodinfo.


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 -