5

Is it possible, in C#, to share code between Visual Studio projects without needing to distribute a DLL?

I'm maintaining some software that's composed of a few Visual Studio C# projects, each building to a simple console executable. There's a lot of shared code between the projects that I'd like to move out. I believe I can put the code in a Class Library project, but I'd rather avoid tacking on a DLL to distribute with each of the executables.

Is there any way around this? I'm new at C#, so perhaps I'm thinking about this all wrong anyway - are there any alternate suggestions?

Eric
  • 5,842
  • 7
  • 42
  • 71
  • 2
    You can add existing files as "linked", that can be a code file. – harold Oct 05 '11 at 16:05
  • 6
    If you're new to an area (C#) then take this advice: do it the "normal" way before you do it the "odd" way. Use a DLL. – John Saunders Oct 05 '11 at 16:05
  • How about sharing the "lib"-code as a specific repository used by your "main-app" repositories? Of course, using a specific .dll version for each project would make things easier. – mbx Oct 05 '11 at 16:16

9 Answers9

4

You can use ILMerge to combine the class library with the exe.

http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx

Sean Kearney
  • 4,008
  • 20
  • 29
3

Well, my personal preference would be to bite the bullet and distribute the DLL as well. It's the cleanest and most idiomatic option.

Other options:

  • You can add a "link" to an existing source file outside the project directory if I remember correctly, but it'll be somewhat odd.
  • You could use ilmerge to merge the class library into the executable as a post-build step
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

You could add the code as linked files in visual studio - right click on the project, choose "Add/existing item" and on the "Add" button, click the drop down arrow and choose "Add as link".

That creates a shortcut to the added file within the project rather than copying it, which means you only maintain one source file but still get it compiled in where it's needed.

I think a class library would be neater, but if you want to avoid that then shortcuts are an easy option.

MarcE
  • 3,586
  • 1
  • 23
  • 27
  • I've seen a few cases where this was actually cleaner. – Joshua Oct 05 '11 at 16:08
  • I've seen it used to share data transfer objects between client and server code which worked well - but I've also seen situations where an update to shared code has killed related projects! So use with caution... – MarcE Oct 05 '11 at 16:12
  • Of course it could. Same way an old-school static library was still a joint dependency. – Joshua Oct 05 '11 at 16:14
  • This is intriguing... a couple of things concern me, though. Suppose I have a large code base, with lots of interdependencies. If I want to use something in one of my projects, would I need to find all of the dependencies, and add those as linked files as well? And could my code base's code organization (folder structure) automatically replicate, or would I need to replicate that manually in each project? – Eric Oct 05 '11 at 16:20
  • Say you have a common file "foo.cs". It physically exists in one project (or, potentially, in no projects and just loosely in a folder of common sources). When you want that function, you include "foo.cs" as a link into the project that needs it. If "foo.cs" needs something in "bar.cs" then yes, you need to add that as well. Organisationally, links exist as shortcuts wherever you add them under each project - you could create a different structure for each project if you wanted. – MarcE Oct 06 '11 at 09:37
1

One option is to use ilmerge.

ilmerge /target:winexe /out:SelfContainedProgram.exe Program.exe ClassLibrary1.dll ClassLibrary2.dll

Compile your application and class library as normal, then merge the class libraries with the program later. You could even just have a post build step to do this for you.

Wood
  • 729
  • 3
  • 5
0

When you Add a file to a project, there is a drop down on the add button to add it as linked content.

Then changes in the original location are reflected in the linked one.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
0

I'm not sure what you're ideal scenario is? If you don't want to distribute any additional files then the code has to reside in your exe. You can reference one exe from another, but then if exe A references exe B that means you can't distribute A without also distributing B.

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
0

You probably want to embed or merge the class library assembly within the assembly of your executable. This has been discussed before. Try here: Embedding assemblies inside another assembly

Community
  • 1
  • 1
ChrisBD
  • 9,104
  • 3
  • 22
  • 35
0

It is possible to share code between projects. Simply create a VS.NET solution. Add each project to the solution. Apply appropriate project references. Your single VS.NET solution can have n projects that build to console applications. That's by far the simplest way to share code between projects.

Shan Plourde
  • 8,528
  • 2
  • 29
  • 42
0

In addition to all of the other great methods posted, you could package the DLL as an embedded resource of your EXE and load the assembly in memory at runtime using Assembly.Load(byte[]).

Steve Danner
  • 21,818
  • 7
  • 41
  • 51