c# - Is it possible to hook up a anonymous Delegate using reflection? -
from msdn article, there's quite few ways hook delegate using reflection.
it seems best way createdelegate method:
delegate d = delegate.createdelegate(delegatetype, targetobject, handlermethodname);
under normal circumstances, i'd pointing handler method within targetobject class. if delegate created anonymously? example:
public delegate void selectedvehiclescollectiondelegate(string query, list<vehicles> list); ... myobject.selectedvehiclescollection = (query, list) => { //assign list of vehicles list matching query };
there's not method within class definition delegate referencing. need invoke delegate unknown @ runtime, obtaining list of items result. ok, looks terminology got best of me. wasnt aiming @ creating handler invoke what's there (tomas petricek's answer still gives me insight though).
given comment, sounds calling delegate rather creating one, in case delegate.dynamicinvoke
you're after, if don't know relevant delegate type @ compile time.
if do know relevant delegate type, not property name, can cast:
mydelegate handler = (mydelegate) propertyinfo.getvalue(obj, null); handler(...); // call normal
Comments
Post a Comment