将自定义文件名设置为引用的dll

时间:2013-09-30 12:02:50

标签: c# dll

我有一个应用程序,它引用程序集“Library.dll”。我将此程序集的名称更改为“Library222.dll”,现在我的应用程序失败,并出现“无法加载文件或程序集......”的异常 如何在运行时指定此dll文件的新名称“Library222.dll”? 我发现了这个问题Set Custom Path to Referenced DLL's? 但是指定文件夹到dll,而不是文件名。我没有改变dll的路径,我改变了文件名,所以我需要指定文件名。

3 个答案:

答案 0 :(得分:4)

仅通过重命名程序集无法实现此目的。

程序集的名称在编译时写入其元数据。
更改文件名时,实际上不会更改元数据中的名称。

您必须取消引用Library.dll,并引用Library222.dll,然后重新编译。

答案 1 :(得分:2)

我找到了这个简单的解决方案!事件AppDomain.AssemblyResolve帮助我解决了问题

using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace TestAsembly
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            //bla-bla...
        }

        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            if (args.Name.StartsWith("Library,"))
            {
                Assembly asm = Assembly.LoadFrom("Library222.dll");
                return asm;
            }
            return null;
        }
    }
}

答案 2 :(得分:2)

试试这个项目属性 - >单击应用程序,然后更改程序集名称字段。在早期版本中,它可能位于不同的位置,但我认为Assembly Name字段仍然是您正在寻找的字段。