messaging - Is there a way to call private functions of a form or class through Type.InvokeMember or messages in C#? -
what have bunch of forms in main application have both private , public functions. have plugin architecture in place accesses each form when created , loaded , holds reference updating it, adding controls, etc.
what trying implement plug in architecture, plugins may need call form's private functions. here example of tried type.invokemember:
public partial class form1 : form { form1() { initializecomponent(); } private void sayhello() { messagebox.show("hello world!"); } }
in dll...
public class pluginclass { form1 myreferencedform1; pluginclass() { //constructor code here... //also sets reference existing form1 instance } private callmember() { type t = typeof(form1); //i guess can call 'myreferencedform1.gettype();' here t.invokemember("sayhello", system.reflection.bindingflags.invokemethod | system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.public, null, myreferencedform1, new object[] { }); } }
i have tried "sayhello"
, "sayhello()"
, both return 'missingmethodexception' error:
method 'form1.sayhello()' not found.
do need create , use binder? if how do that? can easier system.windows.forms.message? if how?
you haven't included bindingflags.instance
in list of flags... doesn't have either instance or static methods check!
personally call getmethod
, methodinfo.invoke
separate discovery of method invocation. find makes easier debug, ymmv.
complete sample:
using system; using system.reflection; class otherclass { private void foo() { console.writeline("otherclass.foo"); } } class test { static void main() { otherclass target = new otherclass(); typeof(otherclass).invokemember("foo", bindingflags.invokemethod | bindingflags.instance | bindingflags.public | bindingflags.nonpublic, null, target, new object[0]); } }
or using "separate fetch invocation":
using system; using system.reflection; class otherclass { private void foo() { console.writeline("otherclass.foo"); } } class test { static void main() { otherclass target = new otherclass(); methodinfo method = typeof(otherclass).getmethod("foo", bindingflags.invokemethod | bindingflags.instance | bindingflags.public | bindingflags.nonpublic); // check method being null etc method.invoke(target, null); } }
Comments
Post a Comment