简体   繁体   中英

C# use class from another project - add reference

I have a problem with my solution in c#, inside the solution I have 2 projects project1 and project2, I have a problem because I use within project1 with functions that are within project2 so I need reference to project2.

after I build the solution I see in bin/debug folder dll file of project2.

how can I use functions from project2 but without creating the dll file?

I try to click right on the specific reference in project1 and go to properties and change copy local to false and dll not show in folder but when I run the code I get assembly error. how can I solve it?

thanks!

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM