合并程序集中的.NET类型定义(ILMerge) [英] .NET type definitions in merged assemblies (ILMerge)

查看:48
本文介绍了合并程序集中的.NET类型定义(ILMerge)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ILMerge合并几个.NET程序集,包括一些第三方程序集.自从这样做以来,我经历了几个错误,这些错误都归结为以下事实:类型定义与定义它们的程序集相关.

I am merging several .NET assemblies using ILMerge including some 3rd party assemblies. Since doing so, I've experienced several errors that all boil down to the fact that type definitions are tied to the assembly they are defined in.

一个简单的示例是App.config中的log4net config节定义.它使用type ="log4net.Config.Log4NetConfigurationSectionHandler,log4net",因为一旦将log4net程序集合并到我的合并程序集中,该程序将不存在,它将不起作用.不过没什么大不了,我将程序集名称更改为合并的程序集,并且工作正常.

A simple example is the log4net config section definition in my App.config. It uses type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" which will not work since the log4net assembly does not exist once it has been merged into my merged assembly. Not a big deal though, I change the assembly name to my merged assembly and it works fine.

一个稍微复杂的例子是二进制序列化类型.我的系统使用二进制序列化在进程之间发送某些对象.所有可序列化的对象都在所有其他项目引用的通用程序集中定义.我使用的是默认的二进制序列化,但是在反序列化对象时它开始失败,并显示错误消息,指出找不到序列化对象的合并程序集.再说一次,没什么大不了的,我实现了一个自定义的SerializationBinder,它可以在任何加载的程序集中查找类型,而不仅仅是给定的程序集.

A slightly more complicated example is binary serialized types. My system uses binary serialization to send certain objects between processes. All of the serializable objects are defined in a common assembly that all of the other projects reference. I was using the default binary serialization, but it started failing when deserializing the objects with the error stating that it could not find the merged assembly that serialized the object. Again, not a big deal, I implemented a custom SerializationBinder that looks for the type in any loaded assembly, not just the one given.

当序列化类型引用其他可序列化类型时,前面的示例变得更加复杂.我继续遇到越来越多的问题,这些问题变得越来越难以处理.

The previous example got more complicated when the serialized type reference other serializable types. I've continued to run into more and more problems that are getting increasingly difficult to deal with.

我想在这里指出的是.NET类型系统和ILMerge似乎不能很好地配合使用.是否有人对解决此问题有任何经验?是否可以告诉.NET运行时,我不在乎该类型表示应该在哪个程序集中,而只需在任何地方查找它即可?

The point I'm trying to get at here is that the .NET type system and ILMerge don't appear to play well together. Does anyone have any experience with how they have solved this problem? Is it possible to tell the .NET runtime that I don't care what assembly the type says it should be in, just look for it anywhere?

注意:请不要回答问我为什么要合并程序集,这不是这个问题的重点.

NOTE: Please do not reply asking why I'm merging assemblies, that is not the point of this question.

推荐答案

是的,此问题有解决方案:构建模块而不是程序集!

Yes, there is a solution to this problem: Build modules instead of assemblies!

.net的编译器具有一个选项(对于C#和VB为/target:module),该选项可生成模块而不是程序集.然后可以将多个模块传递给编译器,并用于构建最终程序集.

Compilers for .net have an option (/target:module for C# and VB) that builds a module instead of an assembly. Multiple modules can then be passed to the compiler and used to build the final assembly.

当然,所有这些都假设您已经有了第3方程序集的源代码.如果您做不到,也许可以从第三方采购.netmodule版本的第三方程序集?

Of course, this all assumes that you've got the source to those 3rd party assemblies. If you can't get that, perhaps a .netmodule version of the 3rd party assembly can be procured from the 3rd party?

如果那行不通,您仍然有最后一个选择.显然,您已经将第3方程序集分解为IL.从该IL文件中删除程序集信息,并使用"ilasm/dll"构建一个.netmodule,您现在应该可以像使用其他任何.netmodule一样使用该模块!

If that won't work, you've still got one last option. Obviously you're already disassembling a 3rd party assembly into IL. Strip out the assembly information from that IL file and use "ilasm /dll" to build a .netmodule which you should now be able to use just like any other .netmodule!

通过使用模块而不是程序集,您不应再遇到基于程序集的问题.

By using modules instead of assemblies you shouldn't have any more assembly-based issues.

是的,我们已经不再使用ILMerge解决了您使用ILMerge的问题,但这不是最好的解决方案吗?

True, we've solved your problem with ILMerge by not using ILMerge anymore, but isn't that really the best solution?

希望它对您有用,这里有一些方便的链接:
.netmodule而不是程序集
带有.netmodules的ILASM

Hope it works for you, here's some handy linkage:
.netmodule instead of assembly
ILASM with .netmodules

(而且我在这里认为我对.netmodules的经验永远不会对任何人有用!)

(And here I was thinking that my experience with .netmodules would never be useful to anyone!)

这篇关于合并程序集中的.NET类型定义(ILMerge)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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