Question

I'm trying something exploratory:

Say I have a library "coolproject" and it has dependencies to one.dll, two.dll, and three.dll.

I would like to use ILMerge to combine coolproject with one.dll and two.dll but NOT three.dll. At the completion of the merge I should have coolproject.dll and three.dll. Wherever I intend to use coolproject.dll I must also reference three.dll

Is this even possible? Whenever I try it I get. Unresolved assembly reference not allowed: three. I'm omitting three.dll by setting "Copy Local" = false.

[EDIT]:

Executed as the following:

ILMerge /targetplatform:v2 /log /internalize /out:bin\coolproject.dll obj\Debug\coolproject.dll C:\Users\Nick\Projects\test\bin\one.dll C:\Users\Nick\Projects\test\bin\two.dll
Was it helpful?

Solution

The reason this does not work ended up being rather simple: ILMerge must be able to locate the dll that you have omitted from the list.

When evaluating dependencies in the target library, ILMerge by default checks various locations to identify the dependent library (\bin, GAC, etc) even if you omitted it from the command line list. If it cannot locate this library you must specify its location using the \lib switch. Otherwise you will see the Unresolved assembly reference not allowed: three error.

Example:

ILMerge /lib:..\three\bin\three.dll /targetplatform:v2 /log /internalize /out:bin\coolproject.dll obj\Debug\coolproject.dll C:\Users\Nick\Projects\test\bin\one.dll C:\Users\Nick\Projects\test\bin\two.dll

OTHER TIPS

I have written a little tool to deal with this.

The source code can be found @ https://github.com/leppie/ReferenceRemover.

In you case it will be used as:

ReferenceRemover three.dll "(one|two).*" coolproject.dll

Explanation of args:

  1. Assembly to inspect
  2. Regex to match references
  3. The assembly it need to redirect to

The output is three.dll with its references adjusted.

I think you're looking for the 'exclude' parameter for the /internalize switch. For example, if you use

/internalize:excludes.txt

where the file excludes.txt contains

three.dll

it will internalize one.dll and two.dll but leave three.dll as an external dependancy.

Here are a couple of blog posts that go into further detail:

Update: From the documentation:

2.10 ExcludeFile

public string ExcludeFile { get; set; }

This property is used only in conjunction with the Internalize property (Section 2.12). When this is set before calling Merge, it indicates the path and filename that will be used to identify types that are not to have their visibility modified. If Internalize is true, but ExcludeFile is "", then all types in any assembly other than the primary assembly are made non-public. Setting this property implicitly sets Internalize to true.

The contents of the file should be one regular expression per line. The syntax is that defined in the .NET namespace System.Text.RegularExpressions for regular expressions. The regular expressions are matched against each type's full name, e.g., "System.Collections.IList". If the match fails, it is tried again with the assembly name (surrounded by square brackets) prepended to the type name. Thus, the pattern “[A].*” excludes all types in assembly A from being made non-public. (The backslashes are required because the string is treated as a regular expression.) The pattern “N.T” will match all types named T in the namespace named N no matter what assembly they are defined in.

It is important to note that the regular expressions are not anchored to the beginning of the string; if this is desired, use the appropriate regular expression operator characters to do so.

2.12 Internalize

public bool Internalize { get; set; }

This controls whether types in assemblies other than the primary assembly have their visibility modified. When it is true, then all non-exempt types that are visible outside of their assembly have their visibility modified so that they are not visible from outside of the merged assembly. A type is exempt if its full name matches a line from the ExcludeFile (Section 2.10) using the .NET regular expression engine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top