DLL加载

时间:2016-11-28 16:50:24

标签: c# wpf exception mef

我试图用MEF实现一个插件框架。我有3个项目:

  • 主持人项目(WPF)
  • 接口定义项目(可移植类库)
  • 插件项目(便携式类库)

现在在主机中,我尝试加载插件组件dll(仅显示应该加载dll的类):

public class SafeDirectoryCatalog : ComposablePartCatalog
{
    private readonly AggregateCatalog _catalog;

    public SafeDirectoryCatalog(string directory)
    {
        var files = Directory.GetFiles(directory, "*.dll", SearchOption.AllDirectories);

        _catalog = new AggregateCatalog();

        foreach (var file in files)
        {
            try
            {
                var asmCat = new AssemblyCatalog(file);

                if (asmCat.Parts.ToList().Count > 0)
                {
                    _catalog.Catalogs.Add(asmCat);
                }

            }
            catch (ReflectionTypeLoadException)
            {
            }
            catch (BadImageFormatException)
            {
            }
        }
    }
    public override IQueryable<ComposablePartDefinition> Parts
    {
        get { return _catalog.Parts; }
    }
}

var asmCat = new AssemblyCatalog(file);

我可以看到,有一个&#34; ReflectionTypeLoadException&#34;和零件清单es emtpy:

Exception Screenshot (VS is German)

这是我的接口定义(输出主机和插件项目中引用的dll):

namespace HCInterfaces
{
    public interface HomeControlInterface
    {
        string GetModuleName();
    }
}

最后这是我输出plugin.dll的插件类:

using HCInterfaces;
using System.Composition;

namespace Plugin2
{
    public partial class MainWindow
    {
        public MainWindow()
        {

        }


        [Export(typeof(HomeControlInterface))]
        class BMW : HomeControlInterface
        {
            public string GetModuleName()
            {
                return "hännschenklein";
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

问题可能是图书馆参考不兼容 也许这个链接会有所帮助:

MEF 2 Composition with .NET 4.5, Windows Store PCL

相关问题