简体   繁体   English

C#在共享项目中使用引用/类库

[英]C# Using references/class library in a shared project

I have just learned and created a shared project in visual studio 2017. I have noticed that the shared project did not have the "Reference" to refer to other resources (other projects, class library, …). 我刚刚在Visual Studio 2017中学习并创建了一个共享项目。我注意到该共享项目没有引用其他资源(其他项目,类库等)的“参考”。 I even take a look at the .shproj file and saw that it only Import the class I have created inside the shared project. 我什至看一下.shproj文件,发现它仅导入我在共享项目中创建的类。

My problem is that if I want to create an add-in app, I need use the class library to call the necessary API that is exposed by the origin software. 我的问题是,如果要创建外接程序,则需要使用类库来调用原始软件公开的必要API。 How can I reference/add other project/ class library (or resources in general) to my shared project? 如何将其他项目/类库(或一般资源)引用/添加到共享项目中? Or is that even possible at all? 还是完全有可能?

Part of my problem is also described here reference to a shared project from other shared project 我的问题的一部分也在此处描述,该引用来自其他共享项目中的一个共享项目

But I need a more general solution. 但是我需要一个更通用的解决方案。 Thank you all for your help. 谢谢大家的帮助。

Long story short: shared projects don't, and realistically can't work that way. 长话短说:共享项目不行,实际上不能那样做。

In Visual Studio a shared project is just a container for files - source code, resources, etc - that you can add into other projects. 在Visual Studio中,共享项目只是文件的容器-源代码,资源等,您可以将其添加到其他项目中。 This can be useful in some cases when you want to have the same code (and so on) in multiple projects without putting that code into a library. 在某些情况下,如果您想在多个项目中使用相同的代码而不将这些代码放入库中,那么这可能会很有用。

Shared projects do not have references, do not have NuGet packages or anything, just the files that they contain. 共享项目没有引用,没有NuGet包或任何东西,只有它们包含的文件。 They don't even have the configuration data required to compile any source files they contain, and the compiler won't do much validation of the contents if the shared project isn't included into a full project of some sort. 它们甚至不具备编译包含的任何源文件所需的配置数据,并且如果共享项目未包含在某种完整项目中,则编译器将不会对内容进行太多验证。

And since the shared project doesn't have any way of specifying references or packages then you will need to add those references and packages to every project that links to the shared project. 而且由于共享项目没有任何指定引用或程序包的方式,因此您需要将这些引用和程序包添加到链接到共享项目的每个项目中。 The compiler will tell you pretty quick if you miss one. 如果您错过了,编译器会很快告诉您。

While it would be nice to have references in shared projects, it turns out to be much less simple than you might think. 虽然在共享项目中有引用会很好,但事实却比您想象的要简单得多。 The same shared project can be included in projects that target different frameworks, platforms and architectures. 同一共享项目可以包含在针对不同框架,平台和体系结构的项目中。 Let's say you're building some code that will run on iOS, Mono, Windows .NET Framework and .NET Core, with specific code for each target and some shared code. 假设您要构建一些将在iOS,Mono,Windows .NET Framework和.NET Core上运行的代码,并为每个目标提供特定的代码,并提供一些共享代码。 If you try to add a NuGet dependency to the shared project it's going to blow up in your face on at least one of those. 如果您尝试将NuGet依赖项添加到共享项目中,那么至少其中之一将在您的面前大张旗鼓。 Same with most of the references. 与大多数参考文献相同。 Add all the references you need for .NET Core and suddenly the other projects don't compile. 添加您需要的所有.NET Core引用,突然其他项目无法编译。

You need to add the reference in the project which consumes the shared project. 您需要在使用共享项目的项目中添加引用。

As an example, let's say you have "Project A" which references "Shared Project B", and you need to use Newtonsoft.Json in "Shared Project B". 例如,假设您有一个引用了“共享项目B”的“项目A”,并且需要在“共享项目B”中使用Newtonsoft.Json。 Since you can't add a reference to the shared project, you install the Newtonsoft Nuget package to Project A and your code in "Shared Project B" will automagically compile. 由于无法添加对共享项目的引用,因此将Newtonsoft Nuget软件包安装到项目A,并且“共享项目B”中的代码将自动编译。

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

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