c# - mef - how to make recomposition work automatically? -
i've been trying recomposition work no luck... tried many times , many approches - no luck... can point out mistake? expect after drop new .dll plugins directory senders collection automatically repopulated new stuff...
//exported classes [export(typeof(isender))] public class smtp : isender { public string name { { return "smtp plugin"; } } public void send(string msg) { } } [export(typeof(isender))] public class exchange : isender { public string name { { return "exchange plugin"; } } public void send(string msg) { // .. blah } }
/---------------------------------------------------------------------
/// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { private const string str_pugins = ".\\plugins"; [importmany(typeof(isender), allowrecomposition = true)] private list<isender> senders; private directorycatalog d; compositioncontainer c; public mainwindow() { initializecomponent(); listbox1.displaymemberpath = "name"; configplugins(); bindsenders(); } private void configplugins() { directoryinfo dir = new directoryinfo(str_pugins); if (!dir.exists) dir.create(); d = new directorycatalog(str_pugins); d.changed += new eventhandler<composablepartcatalogchangeeventargs>(d_changed); c = new compositioncontainer(d); c.exportschanged += new eventhandler<exportschangeeventargs>(c_exportschanged); c.composeparts(this); } void d_changed(object sender, composablepartcatalogchangeeventargs e) { bindsenders(); messagebox.show("d_changed " + (senders == null ? 0 : senders.count)); } private void bindsenders() { listbox1.itemssource = senders; } void c_exportschanged(object sender, exportschangeeventargs e) { bindsenders(); messagebox.show("c_exportschanged "+ (senders == null ? 0 : senders.count)); } }
after response ok, i've added refresh, still don't why listbox won't populate new data...
public partial class mainwindow : window { private const string str_pugins = ".\\plugins"; [importmany(typeof(isender), allowrecomposition = true)] private list<isender> senders; directorycatalog d; compositioncontainer c; public mainwindow() { initializecomponent(); listbox1.displaymemberpath = "name"; configplugins(); bindsenders(); } private void configplugins() { directoryinfo dir = new directoryinfo(str_pugins); if (!dir.exists) dir.create(); d = new directorycatalog(str_pugins); c = new compositioncontainer(d); c.composeparts(this); } private void bindsenders() { label1.datacontext = senders; listbox1.itemssource = senders; } private void button1_click(object sender, routedeventargs e) { d.refresh(); bindsenders(); } }
you have call refresh yourself. if want can use filesystemwatcher object notified when directory contents have changed.
Comments
Post a Comment