c# - How do you call a method by its "name"? -
what way call method name, "method1", if i've got object
, it's type
?
i want this:
object o; type t; // @ point know, 'o' has // 't' it's type. // , know 't' has public method 'method1'. // so, want like: reflection.callmethodbyname(o, "method1");
is somehow possible? realize slow, it's inconvenient, unfortunately i've got no other ways implement in case.
if concrete method name known @ runtime, can't use dynamic , need use this:
t.getmethod("method1").invoke(o, null);
this assumes, method1
has no parameters. if does, need use 1 of overloads of getmethod
, pass parameters second parameter invoke
.
Comments
Post a Comment