Reputation: 75
I have a problem with my solution in C#.
Inside the solution I have 2 projects, Project1 and Project2. I have a problem because in Project1 I use functions that are within Project2 so I need to reference those functions from within Project2.
After I build the solution I see in bin/debug folder a dll file of Project2.
How can I use functions from Project2 without creating the dll file?
I tried to right click on the specific reference in Project1 and go to Properties and change "copy local" to "false" and the dll didn't show up in the folder, but when I run the code I get an assembly error. How can I solve this?
Upvotes: 1
Views: 1655
Reputation: 17668
If I understand your question correctly, you want to include, embed, or statically link, the project2
library into your project1
assembly.
See @Lavamaster's answer for an awesome .NET core 3+ solution.
If possible, it will be painfully complicated.
Tools like ILMerge might be an option for you.
ILMerge is a utility that merges multiple .NET assemblies into a single assembly. It is freely available for use and is available as a NuGet package.
And I also believe some obfuscators are able to merge assemblies into one exe.
Another option is to embed the assembly as resource and load it runtime, but, then you'll need some kind of shared interface definition, most likely in a shared dll.
So, unless really needed; I would recommend to rethink the requirement.
Upvotes: 3
Reputation: 87
If you are using .NET Core 3.0 or higher, you can build the project without including any DLL's and stuff, only having the application executable as the output (meaning you won't see your project DLL, any Nuget package DLL's, etc, only the executable).
All you have to do is open up CMD, cd to the project directory where the .csproj file is, and run this command:
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true
If you need to publish x86, I think you have to change win-x64
to either win-x86
or something like that.
Edit: I should add that this will increase the file size as its packaging .NET Core into the executable.
Upvotes: 1
Reputation: 617
You can't do it without the DLL file. The DLL is the compiled code of Project 2. You are requiring that as a dependency for Project 1, so the Project 2 DLL is going to be there. You could create a Project 3 that holds any shared code, but then you would have a DLL for Project 3 in both your Project 1 and Project 2 bin/debug folder.
Upvotes: 0