简体   繁体   English

使用通过ILMerge创建的库中的程序集

[英]Use assembly from a library which has been created through ILMerge

I have created an assembly package using the tool ILMerge. 我已经使用工具ILMerge创建了一个程序包。

There is an assembly (lets call it A ) which is part of that packaged assembly which is going to be used by a other assembly, which is not included in the package (lets call it B ). 有一个程序集(称为A )是该打包的程序集的一部分,该程序集将由另一个程序集使用,而该程序集不包含在该程序包中(称为B )。

Now what I want to do is create a project which references both, the packed assembly and B . 现在我要做的是创建一个引用打包的程序集和B Now I would like to do that: 现在,我想这样做:

public void Foo()
{
  var obj = new Bar(); // Bar is part of `A`
  var someFactory = new Factory(); // is part of `B`
  someFactory.DoSomething(obj); 
  // compiler error here, which says I need to reference the assembly which contains `Bar`
}

I made sure that the assembly A , which was included into the package and the one referenced by B are the same. 我确保包装中包含的程序集AB引用的程序集相同。

Is there something I`m missing here? 我在这里想念什么吗?

Update with more Context 更新更多上下文

We have a datamodel project which has lots of dependent projects (I know this is bad in the first place, but its legacy code :-( ) So I would like to merge all these assemblies to one in order to use that data model assembly more easily in multiple solutions. 我们有一个数据模型项目,其中有很多相关项目(首先我知道这很不好,但是它的遗留代码:-(),所以我想将所有这些程序集合并为一个,以便更多地使用该数据模型程序集在多种解决方案中轻松实现。

B references A , not whatever freaky deaky merged assembly you have concocted. B引用A ,而不是您构想的任何怪异的,笨拙的合并程序集。 It may contain all of the types of A , but it is not A -- assembly identity matters. 它可能包含A所有类型,但不是 A -程序集标识很重要。 MyMergedPackage.Bar is not A.Bar , even if they use the exact same type name down to the namespace. MyMergedPackage.Bar不是A.Bar ,即使他们使用与名称空间完全相同的类型名称。

There are multiple possible solutions. 有多种可能的解决方案。

  • First and most obviously, you could simply merge B as well. 首先也是最明显的是,您也可以简单地合并B In a typical ILMerge scenario, you merge all assemblies (including the main executable) into one glorious singularity, so you don't have this problem. 在典型的ILMerge方案中,您将所有程序集(包括主可执行文件)合并到一个光荣的奇点中,因此不会出现此问题。 I'm assuming you have good reasons for not doing this. 我假设您有充分的理由不这样做。

  • You can simply call your merged assembly A , even though it's A plus lots more. 您可以简单地将合并的程序集称为A ,即使它是A加更多功能。 If A has a strong name, you'll need to give that same name (version and all) to your merged assembly. 如果A具有强名,则需要为合并的程序集使用相同的名称(版本和全部)。 This will keep B happy, which may be enough, but it won't work if you start adding multiple assemblies that want a piece of the whole (you cannot simply copy A around under different names since the types will not be recognized as the same). 这样可以使B满意,这也许就足够了,但是如果您开始添加想要整体的一部分的多个程序集,那么它将不起作用(您不能简单地以不同的名称来复制A ,因为类型不会被识别为相同的名称)。

  • If your version of .NET is sufficiently recent, you can create a new assembly A that only contains a type forward for Bar to your new assembly. 如果您的.NET版本足够新,则可以创建一个新程序集A ,该程序集仅包含将Bar 转发给新程序集的类型。 This A would simply be a placeholder for the original and only distributed to keep B and assorted friends happy. 这个A只会是原始内容的占位符,并且只会分发给B和其他朋友以使他们开心。 If you have lots of types this way, this is awkward enough that you want automated help. 如果您使用这种方式有很多类型,那么这很尴尬,您需要自动帮助。 I'm not immediately aware of any. 我还没有立即知道。 It also rather defeats the point of merging in most scenarios, since you'll end up with multiple assemblies again anyway. 在大多数情况下,它还是会破坏合并点,因为无论如何您最终都会再次遇到多个程序集。

  • At compile time, simply use the separate assemblies. 在编译时,只需使用单独的程序集。 At deployment, replace them all with your merged assembly. 部署时,将它们全部替换为合并的部件。 Sort things out in code with a handler for AppDomain.AssemblyResolve to fix up the actual type load at run time (simply redirect all unknowns to your merged assembly). 使用AppDomain.AssemblyResolve处理程序在代码中进行分类,以在运行时修复实际的类型加载(只需将所有未知数重定向到合并的程序集)。 This may require some careful tinkering to ensure the event fires before the runtime needs to look up any of the referenced assemblies (static constructors can especially spoil your fun here). 这可能需要谨慎修改,以确保事件运行时需要查找任何引用的程序集之前触发(静态构造函数可能会在这里破坏您的乐趣)。

Disclaimer: I have tested exactly none of these solutions; 免责声明:我没有测试过这些解决方案中的任何一个; if one doesn't work, please keep me posted so I can fix this answer. 如果不起作用,请及时通知我,以便我解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM