开发者

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

开发者 https://www.devze.com 2023-04-11 12:58 出处:网络
Is it possible, in C#, to share code between Visual Studio projects without needing to distribute a DLL?

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?


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

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


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


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.


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.


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.


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消