1

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?

5
  • "how can I use functions from project2 but without creating the dll file?" - unfortunately you can't. You must reference an assembly or duplicate the code in project 1. Commented Jun 23, 2020 at 20:53
  • "how can I use functions from project2 but without creating the dll file?" do ou mean you want to embed (or statically link) the project2 dll within project1? Commented Jun 23, 2020 at 20:53
  • 1
    From the question i see is the same solution, two projects. Duplicated of stackoverflow.com/questions/53342148/… Commented Jun 23, 2020 at 20:54
  • If you just want to avoid multiple files there are ways to merge assemblies, but the compiled code from project 2 would still be part of it. Would that work for you? Commented Jun 23, 2020 at 20:58
  • I think it's not possible. You can create a project3 with the common classes to share between project1 and project2 Commented Jun 23, 2020 at 20:58

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

Serious? Do you have docs on that? Because, it is an awesome feature. :-)
@Stefan here is some more documentation regarding the dotnet publish command: learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.