c# - mef - 如何自动进行重组?

标签 c# plugins refresh mef

我一直在努力让重组工作,但没有运气......我尝试了很多次和很多方法 - 没有运气......任何人都可以指出我的错误吗?我希望在将新的 .dll 放入插件目录后,发件人集合将自动重新填充新内容...

//exported classes
[Export(typeof(ISender))]
public class SMTP : ISender
{
    public string Name
    {
        get { return "SMTP plugin"; }
    }

    public void Send(string msg)
    {

    }
}

[Export(typeof(ISender))]
public class Exchange : ISender
{
    public string Name
    {
        get { return "Exchange plugin"; }
    }

    public void Send(string msg)
    {
        // .. blah
    }
}

/-------------------------------------------- ------------------------

/// <summary>
/// Interaction logic for 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));
    }
}





回应后 好的,我已经添加了刷新,但我仍然不明白为什么列表框不会填充新数据...

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();
    }
}

最佳答案

你必须调用Refresh你自己。如果你愿意,你可以使用 FileSystemWatcher对象在目录内容更改时得到通知。

关于c# - mef - 如何自动进行重组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4186156/

相关文章:

javascript - 从 WordPress 中的自定义插件加载脚本

php - 在 php 中使用元刷新标签进行重定向而不是 header() 函数是一种好习惯吗?

javascript - 添加动态元素时刷新 JQuery 选择器

c# - 无法使用ssh客户端连接mysql数据库

c# - 自动滚动控件(WinAPI)?

eclipse - 从 Eclipse 菜单 Window -> Show View 中删除 View 名称

jenkins - 更新 Jenkins 插件的问题

java - 刷新一些内容,使其在单击删除按钮后消失

c# - Chip8 仿真器 - 用户输入

c# - 使用依赖注入(inject) (Autofac) 并避免服务定位器模式