Eric
Eric

Reputation: 6016

Is it possible to statically share code between projects in C#?

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?

Upvotes: 5

Views: 440

Answers (9)

Daniel Elliott
Daniel Elliott

Reputation: 22867

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.

Upvotes: 0

ChrisBD
ChrisBD

Reputation: 9209

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

Upvotes: 0

Steve Danner
Steve Danner

Reputation: 22158

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[]).

Upvotes: 0

Shan Plourde
Shan Plourde

Reputation: 8726

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.

Upvotes: 0

Wood
Wood

Reputation: 739

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.

Upvotes: 1

MarcE
MarcE

Reputation: 3731

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.

Upvotes: 3

Dylan Smith
Dylan Smith

Reputation: 22245

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500893

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

Upvotes: 3

Sean Kearney
Sean Kearney

Reputation: 4008

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

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

Upvotes: 4

Related Questions