SmartAssembly - 6.0

SmartAssembly

Learning SmartAssembly - 6.0

Merging dependencies

Dependencies merging integrates the code of a dependency with the code of the main assembly. After merging, the dependency is inseparable from the main assembly.

You can obfuscate and prune the code from a merged dependency in the same way as code in the main assembly.

Using dependencies merging

To merge dependencies, in the Project Settings window, go to Dependencies Merging or click the icon in the toolbar.

Select the dependencies to merge. To refresh the list, click Rescan dependencies.

If you select a dependency, which itself has dependencies that are within the list, these are automatically selected.

When dependencies merging is enabled, the colored bars under the toolbar icon and to the left of the features options are green. The colored bars for dependency embedding also turn green when you enable dependencies embedding because merging is preferred over embedding. If merging is disabled, the bars are orange.

Merging example

The following example shows code before and after merging DLL1 and DLL2 into MainExe:

Before merging:

DLL1:

internal class InvalidDocumentContentsException : Exception

public sealed class DiscoveryDocument;

DLL2:

internal class InvalidDocumentContentsException : Exception

internal class LinkGrep

MainExe:

public sealed class DiscoveryClientResult

internal struct CallId

After merging:

MainExe:

public sealed class DiscoveryClientResult

internal struct CallId

internal class InvalidDocumentContentsException : Exception

internal sealed class DiscoveryDocument


internal class InvalidDocumentContentsException : Exception

internal class LinkGrep

What is the difference between merging and embedding?

Merging

When you merge a dependency with the main assembly, the code from the two assemblies are merged into a single assembly. The resulting assembly does not contain a reference to the dependency. Calls are no longer public (by name) but internal (by member ID), which provides greater protection and is quicker.

You can protect a merged dependency by obfuscating the whole assembly in the normal way.

Embedding

When you embed a dependency, it is stored in the main assembly. Optionally, you can also compress and encrypt the dependency's contents. If you embed a dependency, the main assembly is not modified.

At runtime, the first time the DLL is needed, it is unencrypted and decompressed (if applicable) and then an assembly is created and loaded into memory. The embedded assembly keeps its identity and its integrity.

To protect the dependency, first protect it in a separate SmartAssembly project and then embed the obfuscated DLL.

Troubleshooting merging

Third-party DLLs

You may not be able to merge a third-party DLL. In some cases, the DLL has integrity protection and will fail to load if the code is changed, which happens when the dependency is merged with the main EXE. If this happens, embed the dependency instead of merging it (see Embedding dependencies).

Complex merging

Merging may fail if your application has a complex architecture and you have not merged all dependencies together. A typical example of when merging may cause your application to fail is when you are using a plug-in, as in the case below:

Assume that both MainExe.exe and plugin.dll reference shared.dll, and also that MainExe.exe loads plugin.dll.

If you merge shared.dll into MainExe.exe, MainExe.exe and plugin.dll will not be able to communicate using types from shared.dll. This is because the fully-qualified names of those types will have changed from "shared, MyNamespace.MyType" to "MainExe, MyNamespace.MyType".

The solution is to embed shared.dll instead of merging it (see Embedding dependencies).

For projects where dependency embedding is not supported (.NET 1.x, .NET Compact Framework, XNA), you must keep the dependency separate and distribute the DLL with your application.

See also

Embedding dependencies

Was this article helpful?

Search support
Forums
Visit the SmartAssembly forum.

SmartAssembly

all products