Returning a String from a C# WinForm .dll -
i have c# .dll envoked within c# application useing "system.reflection" @ runtime. .dll contains winform class used display information user. .dll envoked using following code:
dll = assembly.loadfrom(strdllpath); classtype = dll.gettype(string.format("{0}.{0}", strnscn)); classinst = activator.createinstance(classtype, paramobj); form dllwinform = (form)classinst; dllwinform.showdialog();
now, problem want return string winform .dll. error or show process completed correctly. know how done when calling method within requested .dll, follows:
system.reflection.assembly loadedassembly = system.reflection.assembly.load("mscorlib.dll"); system.console.writeline(loadedassembly.getname()); object myobject = loadedassembly.createinstance("system.datetime", false, bindingflags.exactbinding, null, new object[] {2000, 1, 1, 12, 0, 0}, null, null); methodinfo m = loadedassembly.gettype("system.datetime").getmethod("tolongdatestring"); string result = (string) m.invoke(myobject, null);
but how do case of winform called .dll @ runtime?
any suggestions appreciated.
okay, boil question , comments down, we're trying here have c# app loads dll implemented 3rd party @ later date, , app needs status information component in loaded dll (the fact component uses winforms vs. other ui seems inconsequential).
the best way start out interface or base class can shared between hosting application , loaded component. in order achieve this, interface needs in separate dll. first create class library project , add following class:
using system; using system.windows.forms; namespace simplepluginshared { public class pluginbase : form { public virtual string getstatus() { return null; } } }
then add reference class library project implements component you're loading via reflection (or share third party them implement). here example implementation of plugin base:
using system; using system.windows.forms; using simplepluginshared; namespace simplepluginexample { public partial class myform : pluginbase { private string _status = "unspecified"; public myform() { initializecomponent(); } public override string getstatus() { return _status; } private void btngive_click(object sender, eventargs e) { _status = "give him stick."; this.dialogresult = dialogresult.ok; this.close(); } private void btndontgive_click(object sender, eventargs e) { _status = "don't give him stick!"; this.dialogresult = dialogresult.cancel; this.close(); } } }
and lastly code load , call component:
using system; using system.linq; using system.reflection; using system.windows.forms; using simplepluginshared; namespace simplepluginhost { public partial class mainform : form { public mainform() { initializecomponent(); } private void btnbrowse_click(object sender, eventargs e) { openfiledialog openplugindlg = new openfiledialog() { defaultext = "dll", multiselect = false, title = "open plugin dll", filter = "dlls|*.dll" }; if (openplugindlg.showdialog() == dialogresult.ok) { txtpluginpath.text = openplugindlg.filename; } } private void btngo_click(object sender, eventargs e) { assembly plugindll = assembly.loadfrom(txtpluginpath.text); type plugintype = plugindll.gettypes().where(t => typeof(pluginbase).isassignablefrom(t)).first(); pluginbase plugininstance = (pluginbase)activator.createinstance(plugintype); plugininstance.showdialog(); messagebox.show(plugininstance.getstatus()); } } }
he screenshots:
Comments
Post a Comment