Question

Lately I've been working on a few little .NET applications that share some common code. The code has some interfaces introduced to abstract away I/O calls for unit testing.

I wanted the applications to be standalone EXEs with no external dependencies. This seems like the perfect use case for static libraries. Come to think of it third party control vendors could benefit from this model too.

Are there some hidden nasties with static libraries that I've missed?

Is there any reason why the C# designers left them out?

Edit: I'm aware of ILMerge but it doesn't offer the same convenience as static libraries.

Was it helpful?

Solution

.NET does in fact support the moral equivalent of a static library. It's called a netmodule (file extension is usually .netmodule). Read more about it in this blog post. Beware that it isn't well supported by the Visual Studio build tool chain. I think extension methods are a problem as well. ILMerge is the better tool to get this done.

OTHER TIPS

This is not directly related to C#, but to the whole .NET umbrella. Sure, it's possible to merge assemblies using ILMerge, but alas it's still a research project, but at least its license permits use for commercial endeavors. Other than that, the runtime (CLR) cannot be coalesced into a single executable, the target platform still needs .NET to be installed.

Edit:

I was typing this before you edited your post with ILMerge. I might be wrong but there is not additional benefits from avoiding the .NET-style dynamic linking at runtime. There's nothing wrong with putting the satellite assemblies together with the executable. Microsoft doesn't recommend putting them in the GAC.

Using static libs has the drawback that it is not patchable by the runtime provider (MS).

For example if you use static MFC libs and a securify issue or other bug is found in the MFC static libs, MS can not patch it (the code is already in your executable). Using shared Dlls allows for an easy patch at OS level without you caring about that.

Of course having shared libs has a small drawback: a dependency is taken and they must be installed on the system.

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