新版本的 dll 出现 FileLoadException

发布于 2024-08-10 23:01:27 字数 202 浏览 3 评论 0原文

我有一个应用程序需要使用两个版本的 dll。我想我只需编译一次应用程序,将 exe 复制到两个目录,然后将两个版本的 dll 复制到每个目录。仅供参考,我没有与 GAC 做任何事情。但是,我在未编译 dll 的版本上收到 FileLoadException。

在项目中,我将 dll 引用的“特定版本”设置为 false。但是,也许这是错误的设置。我如何让它适用于多个版本?

I have an application that needs to work with two versions of a dll. I figured I would just compile the app once, copy the exe to two directories, and then copy the two versions of the dlls to each of these directories. FYI, I'm not doing anything with the GAC. However, I'm getting a FileLoadException on the version with the dll it was not compiled with.

In the project, I have the "Specific Version" set to false for the dll reference. But, maybe that is the wrong setting. How do I get this to work with multiple versions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

万劫不复 2024-08-17 23:01:28

我反对更改配置文件,只是因为这是我需要做的另一件事。关于不同版本的 dll 的地址更改,我认为我没有这个问题,因为我实际上是使用接口来调用我的方法。

更详细地说,我只需要在多个 dll 中创建一种类型的实例。从那里开始,我将其视为接口对象。所以,这是我的解决方案:

Assembly asm = Assembly.LoadFrom("XXX.YYY.ZZZ.Bin.dll");
Type type = asm.GetType("MyType");
IMyType obj = Activator.CreateInstance(type) as IMyType;

这似乎有效。

I ruled against changing the config file, simply because it's another thing I would need to do. In regards to the address changing for different versions of the dll, I don't think I have that problem since I am actually using an interface to call my methods.

To give a little more detail, I simply need to create an instance of one type in multiple dlls. From there, I treat it as an interface object. So, here is my solution:

Assembly asm = Assembly.LoadFrom("XXX.YYY.ZZZ.Bin.dll");
Type type = asm.GetType("MyType");
IMyType obj = Activator.CreateInstance(type) as IMyType;

This seems to work.

故人爱我别走 2024-08-17 23:01:27

如果您引用的程序集经过强签名,并且其版本与编译时所针对的版本不同,则 CLR 将不会加载该程序集。在这种情况下,您可以使用配置文件来指定您想要使用的版本:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="YouAssemblyNameWithoutDotDll"
                              publicKeyToken="your-assembly-token"
                              culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0"
                             newVersion="2.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

当然,这假设程序集的新版本具有相同的方法签名,否则您将得到 MissingMethodException 在运行时。

If the assembly you are referencing is strongly signed the CLR will not load it if it has a different version than the one it was compiled against. You could in this case use the config file to specify the version you would like to use:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="YouAssemblyNameWithoutDotDll"
                              publicKeyToken="your-assembly-token"
                              culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0"
                             newVersion="2.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

Of course this assumes that the new version of the assembly has the same method signatures or you will get a MissingMethodException at runtime.

岁吢 2024-08-17 23:01:27

这是因为每次重新编译dll时,函数的地址都会改变。由于您在编译时将应用程序与 dll 绑定在一起,因此这些地址将在您的应用程序中设置为固定。基本上,您每次都必须使用新版本的 dll 重新编译程序(或者更准确地说,当您弄乱数据结构时)或使用动态加载和 GetProcAddress。

This is because every time you recompile dll, adresses of functions are changed. And since you are binding your app on the compile time with your dll, those adresses are set to be fixed in your app. Basically you must recompile your program everytime with new version of dll (or to be more precise, when you mess with datastructures) or use dynamic loading and GetProcAddress.

╭ゆ眷念 2024-08-17 23:01:27

确保程序集的两个版本具有在 AssemblyInfo.cs 文件中指定的相同的 AssemblyVersion。

例如:

[assembly: AssemblyVersion("1.0.0.0")]

我相信 VS 2008 之前为新项目生成的默认值是:

[assembly: AssemblyVersion("1.0.0.*")]

每次构建时都会自动增加程序集版本。

这对于已签名的程序集尤其重要,因为程序集的强名称包括它的版本。

Make sure that the two versions of the assembly have the same AssemblyVersion specified in your AssemblyInfo.cs file.

Ex:

[assembly: AssemblyVersion("1.0.0.0")]

I believe the default generated for new projects before VS 2008 is:

[assembly: AssemblyVersion("1.0.0.*")]

which will automatically increment the assembly version every time it is built.

This is particularly important for assemblies that are signed since an assembly's strong name includes its version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文