【问题标题】:ReflectionTypeLoadException on DLL loadDLL 加载时的 ReflectionTypeLoadException
【发布时间】:2016-11-28 16:50:24
【问题描述】:

我正在尝试使用 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);

我可以看到,有一个“ReflectionTypeLoadException”和零件清单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";
            }
        }

    }
}

【问题讨论】:

    标签: c# wpf exception mef


    【解决方案1】:

    问题可能是库引用不兼容。
    也许这个链接会有所帮助:

    MEF 2 Composition with .NET 4.5, Windows Store PCL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      相关资源
      最近更新 更多