Cook Computing

This is a useful tip

May 23, 2002 Written by Charles Cook

This is a useful tip from Sean Slavin on the dotnet list:

The IDE does have the option to link in a file. When add an existing item to your project, click the down arrow next to the open button in the dialog. Change it to 'Link'. This will not make a copy in your project directory. The linked file will also be updated whenever you change it from it's original project.

This was in response to the question "How to create a C# source lib?". The consensus of the replies seemed to be that creating an assembly is the better way of doing things but this is not entirely satisfactory in some cases:

  • You don't want to distribute extra assemblies containing library code (for whatever reason, for example to speed up downloading a smart client).
  • You only want to reuse a single class. If your assemblies are very fine grained this means that in other circumstances you will end up distributing many library assemblies whereas if they are coarsely grained you sometimes end up distributing more code than you need.

One of the problems with linking a source file into a project is version control: the source file has to be the correct version. You might not want to use the latest version that is visible in your source tree. Referencing an assembly gives you more control from this point of view

Something like the traditional object library approach would be useful and .NET almost supports this with modules. A file can be compiled to build a module which can then be used when compiling other assemblies. To build a module in C#:

csc /target:module foo.cs

This generates a file called foo.netmodule. To use this when compiling an assembly:

csc /addmodule:foo.netmodule bar.cs

This is also how you can combine code from different .NET languages into a single assembly. Unfortunately VS.NET does not support modules so you have to use a makefile or NAnt if you want to use modules. Something for the next release?