VSIX - 无法加载引用的 dll 的文件或程序集 [英] VSIX - Cannot load file or assembly of a referenced dll

查看:39
本文介绍了VSIX - 无法加载引用的 dll 的文件或程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与这个非常相似,只有答案和解决方法对我不起作用.我也在 Visual Studio 2012 中.

My question is very similar to this one, only that the answer and work-around are not working for me. Also I am in Visual Studio 2012.

我有一个 VSPackage 正在引用另一个项目,该项目依赖于其他 dll.每次我在调试中运行我的包时,我都会收到一个无法找到其他 dll 的异常.它们位于输出目录中,并且已签名.

I have a VSPackage which is referencing another project, which is dependent on other dlls. Everytime time I run my package in debug I get an exception that the other dlls cannot be found. They are in the output directory, and they are signed.

我尝试通过 VSPackage 项目直接引用它们,但无济于事.

I tried referencing them directly by the VSPackage project to no avail.

想法?

推荐答案

存在此问题是因为如果您的扩展程序没有显式依赖这些程序集,Visual Studio 不会在您的扩展程序文件夹中查找程序集.例如,在配置文件(IoC 配置)或 xaml 代码中设置的依赖项.我知道此问题的三种解决方案:

This problem exists because Visual Studio is not looking for assemblies in a folder of your extension if your extension has no explicit dependence on these assemblies. For example, dependences set in a configuration file (IoC config) or in xaml code. I know three solutions to this problem:

  1. 您可以在 GAC 中部署这些程序集,Visual Studio 将加载它们.如果您使用专为在 GAC 中使用而构建的第三方库(例如,MS Enterprise Library),则此方法非常有用.但是 VSIX 部署包 不允许在 GAC 中安装程序集,您可以使用 MSI 安装程序.

  1. You can deploy these assemblies in the GAC and Visual Studio will load them. This method is good if you use a third-party library that built for use in the GAC (for example, MS Enterprise Library). But VSIX Deployment Package does not allow installing assemblies in the GAC, you can use the MSI installer.

对于 Visual Studio 2010/2012 的 VSPackages,您可以使用 ProvideBindingPath 属性.扩展所在的路径将添加到 Visual Studio 用于查找依赖程序集的路径中.如果您的扩展不包含 VSPackage,您可以将此属性添加到任何公共类 (见这里).

For VSPackages for Visual Studio 2010/2012 you can use ProvideBindingPath attribute. The path where your extension located will be added to the paths that Visual Studio uses to find dependent assemblies. If your extension doesn't include a VSPackage, you can add this attribute to any public class (see here).

[ProvideBindingPath] 
public class MyVsPackage : Package 
{ /* ... */ }

  • 您可以手动解析程序集名称.为此,您需要订阅 AssemblyResolve 事件,并且需要从处理程序返回所需的程序集.这是最灵活的方式,如果你不能使用前面的方法,这个特别适合你.

  • You can manually resolve assembly names. To do this, you need to subscribe to the AssemblyResolve event and you need to return required assemblies from a handler. This is the most flexible way, if you cannot use the previous methods, this is especially for you.

    在我的 IntelliDebugger 项目中,我为它编写了一个 ManualAssemblyResolver 类:

    In my IntelliDebugger project, I wrote a class ManualAssemblyResolver for it:

    using System;
    using System.Reflection;
        
    namespace IntelliEgg.Debugger.Utility
    {
        public class ManualAssemblyResolver : IDisposable
        {
            public ManualAssemblyResolver(Assembly assembly)
            {
                if (assembly == null)
                    throw new ArgumentNullException("assembly");
    
                _assemblies = new[] {assembly};
                AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
            }
    
            public ManualAssemblyResolver(params Assembly[] assemblies)
            {
                if (assemblies == null)
                    throw new ArgumentNullException("assemblies");
    
                if (assemblies.Length == 0)
                    throw new ArgumentException("Assemblies should be not empty.", "assemblies");
    
                _assemblies = assemblies;
                AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
            }
    
            public void Dispose()
            {
                AppDomain.CurrentDomain.AssemblyResolve -= OnAssemblyResolve;
            }
    
            private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
            {
                foreach (Assembly assembly in _assemblies)
                {
                    if (args.Name == assembly.FullName)
                    {
                        return assembly;
                    }
                }
    
                return null;
            }
    
            private readonly Assembly[] _assemblies;
        }
    }
    

    此类必须在第一次调用问题程序集之前创建(例如,在 Package::Initialize() 方法中)

    This class must be created before the first call to the problem assembly (e.g., in Package::Initialize() method)

    这篇关于VSIX - 无法加载引用的 dll 的文件或程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆