1

There are a lot of little things I find myself re-writing here and there because they might be too large/complex to represent as a snippet, but realistically it doesn't make sense to make a stand-alone DLL out of it because we might only be talking a few dozen or a few hundred lines of code.

For example a little form which contains only a text box where the user enters a password and closes on {Enter}.

Or an extension method which can serialise/deserialise any object to/from a GZipped file assuming the object is marked as Serializable.

The list goes on. I have accumulated lots of little bits and pieces over the years and it's not organised in any neat way.

In C++ projects, I can write a lib file containing these bits of code which I can add to my compiler settings in such a way that any future C++ project I create has this lib included. I have done this with ATL and Boost.

I don't know of a way to do this for C# projects. Is it possible?

Edit:

If I make an assembly, I have to compile it to a DLL and distribute the DLL alongside my main executable. The DLL may be small or it may be quite large, I don't know. But I may only need to use a few tiny functions in that DLL for my project. In C++, only the functions I use are statically linked when I use the library, however if I distribute my software with a DLL then I have to distribute everything.

I know it is possible to merge the DLL with the main executable so that the user isn't aware that there is a separate library, however the whole DLL is still being packaged along with the executable.

Imagine I write a DLL with lots of my own maths, stats, file IO, image manipulation, serialisation, user IO, etc included. Nothing fancy, just some common things I find myself doing quite frequently. The DLL might be, say, 4MB.

Now I want to write a program which uses a tiny part of the DLL, and if I were to simply copy/paste the necessary code then my EXE would end up being, say, 700kB.

Are you saying that I either copy/paste the code I need, or I have to distribute a 4MB DLL along with my 700kB EXE?

Ozzah
  • 10,631
  • 16
  • 77
  • 116
  • Why doesn't it make sense to create an assembly for these? Seems perfectly reasonable to me. – D Stanley Jul 05 '12 at 03:39
  • Because I want to statically link the things I need. I don't want to distribute with my software, which would normally be a small executable, a separate DLL file just so I can have 1 extra function that I use frequently. – Ozzah Jul 05 '12 at 03:51

3 Answers3

3

Aside from using an assembly, the only way I know of is to create a link in your project to the source code in question. In visual studio the process is:

Project → Add → Existing File → Add As Link (the little down arrow:)

si618
  • 16,580
  • 12
  • 67
  • 84
1

It is not possible at a source code level, although often requested (just Google c# #include equivalent). The only reasonable alternative that c# offers is compiling your common code as a DLL and adding a reference to it.

Note that although you can add a file to your project from another project, it will take a copy and therefore not maintain updates. I have used this to achieve the same effect 'manually' - when the common file is updated, I excluded it from the project 'referencing' it and then re-added to get a fresh copy.

UPDATE As commented below, you can add as a link - how cool! Why did nobody tell me.

Michael
  • 8,891
  • 3
  • 29
  • 42
  • 3
    The later sentence is not true, see my answer, links are possible. I use them for nearly all projects for shared AssemblyInfo. – si618 Jul 05 '12 at 03:49
  • Here's an example of how it can be useful: http://stackoverflow.com/a/471309/44540 – si618 Jul 05 '12 at 04:26
  • Nice to refer to your own past answer - you should have got the acceptance. I gave you a +1 on the comment above and upvoted the answer – Michael Jul 05 '12 at 05:10
  • Thanks, I only stumbled across it by chance and then realised how useful it is for shared common files :) – si618 Jul 05 '12 at 07:15
-1

We add a common directory to the overall includes path, then use

#include <somefile.cpp>

directly in our cpp files. It'll include the source straight in.

Joel Lucsy
  • 8,520
  • 1
  • 29
  • 35