简体   繁体   English

C# 使用来自另一个项目的 class - 添加参考

[英]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.我在 c# 中的解决方案有问题,在解决方案中我有 2 个项目 project1 和 project2,我有一个问题,因为我在 project1 中使用了 project2 中的功能,所以我需要参考 project2。

after I build the solution I see in bin/debug folder dll file of project2.构建解决方案后,我在 project2 的 bin/debug 文件夹 dll 文件中看到。

how can I use functions from project2 but without creating the dll file?如何使用 project2 中的函数但不创建 dll 文件?

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.我尝试右键单击 project1 和 go 中的特定引用到属性并将本地复制更改为 false 并且 dll 不显示在文件夹中,但是当我运行代码时,我得到程序集错误。 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.如果我正确理解您的问题,您希望将project2库包含、嵌入或静态链接到您的project1程序集中。

See @Lavamaster's answer for an awesome .NET core 3+ solution.请参阅@Lavamaster 的答案,了解出色的 .NET 核心 3+ 解决方案。


If possible, it will be painfully complicated.如果可能的话,这将是痛苦的复杂。

Tools like ILMerge might be an option for you.ILMerge这样的工具可能是您的选择。

ILMerge is a utility that merges multiple .NET assemblies into a single assembly. ILMerge 是一个实用程序,可将多个 .NET 程序集合并为一个程序集。 It is freely available for use and is available as a NuGet package.它可以免费使用,并以 NuGet package 的形式提供。

And I also believe some obfuscators are able to merge assemblies into one exe.而且我也相信一些混淆器能够将程序集合并到一个 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.另一种选择是将程序集作为资源嵌入并在运行时加载它,但是,您将需要某种共享接口定义,最有可能在共享 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). 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可执行文件)。

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.你只需要打开CMD,cd到.csproj文件所在的项目目录,然后运行这个命令: dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true如果你需要发布x86,我认为您必须将win-x64更改为win-x86或类似的东西。

Edit: I should add that this will increase the file size as its packaging .NET Core into the executable.编辑:我应该补充一点,这将增加文件大小,因为它将 .NET Core 打包到可执行文件中。

You can't do it without the DLL file.如果没有 DLL 文件,您将无法做到这一点。 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. DLL 是项目 2 的编译代码。您需要将其作为项目 1 的依赖项,因此项目 2 DLL 将在那里。 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.您可以创建一个包含任何共享代码的项目 3,但是您将在项目 1 和项目 2 的 bin/debug 文件夹中为项目 3 创建一个 DLL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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