【问题标题】:Assembly.GetTypes() generating LoaderException ErrorAssembly.GetTypes() 生成 LoaderException 错误
【发布时间】:2012-08-02 04:42:05
【问题描述】:

我尝试了很多谷歌搜索,但未能找到解决方案。

请帮我解决。

我有一个 Windows 应用程序设置向导,它将运行一个安装程序类,其中分配了自定义操作。

该项目采用插件架构。

在安装时,我需要安装打印机驱动程序,安装程序类有代码来安装它,通过调用该插件。

但是,当我尝试检索已加载插件的 GetTypes() 属性时,我收到了加载程序异常错误,安装程序将退出。

如果我运行我的 Windows 应用程序,则 GetTypes() 属性工作正常。

这是我的代码。请查看并检查是否有任何问题。

   private static List<Assembly> LoadPlugInAssemblies()
    {

        DirectoryInfo dInfo = new DirectoryInfo(Path.Combine(Assembly.GetExecutingAssembly().Location.Replace("PluginSDK.dll", ""), "Plugins")); 

        FileInfo[] files = dInfo.GetFiles("*.dll");

        List<Assembly> plugInAssemblyList = new List<Assembly>();

        if (null != files)
        {
            foreach (FileInfo file in files)
            {
                plugInAssemblyList.Add(Assembly.LoadFile(file.FullName));
            }
        }
        return plugInAssemblyList;
    }

    static List<IUserFunctionPlugin> GetPlugIns(List<Assembly> assemblies)
    {
        List<Type> availableTypes = new List<Type>();

        foreach (Assembly currentAssembly in assemblies)
            availableTypes.AddRange(currentAssembly.GetTypes());

        List<Type> pluginList = availableTypes.FindAll(delegate(Type t)
        {
            List<Type> interfaceTypes = new List<Type>(t.GetInterfaces());
            object[] arr = t.GetCustomAttributes(typeof(PluginAttributes), true);
            return !(arr == null || arr.Length == 0) && interfaceTypes.Contains(typeof(IUserFunctionPlugin));
        });

        // CONVERT THE LIST OF OBJECTS TO AN INSTANTIATED LIST OF IPlugins
        return pluginList.ConvertAll<IUserFunctionPlugin>(delegate(Type t) { return Activator.CreateInstance(t) as IUserFunctionPlugin; });
    }

我尝试使用 Assembly.ReflectionOnlyLoad,但结果是同样的错误。 提前致谢!!!

【问题讨论】:

  • 您应该捕获异常并记录/显示/输出它,以便确定原因。
  • 好的@logicnp:这是我收到的日志。无法加载文件或程序集 'PluginSDK, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 或其依赖项之一。该系统找不到指定的文件。融合日志:警告:程序集绑定日志记录已关闭。要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) 设置为 1。注意:与程序集绑定失败日志记录相关的一些性能损失。要关闭此功能,请删除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。

标签: c# assemblies gettype


【解决方案1】:

如果这是程序集绑定错误,请尝试运行 Fuslogvw.exe (Assembly Binding Log Viewer) 以收集有关该错误的更多信息。

【讨论】:

  • 感谢@Jacob:尝试使用程序集绑定日志查看器。但是在安装向导运行时,在查看器中没有找到任何条目。!!!
猜你喜欢
  • 1970-01-01
  • 2011-02-09
  • 2015-03-12
  • 2018-12-05
  • 2014-05-23
  • 2012-09-28
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多