简体   繁体   English

Visual Studio 2015 中的共享项目和类库有什么区别?

[英]What is the difference between a Shared Project and a Class Library in Visual Studio 2015?

I was looking at the new features for Visual Studio 2015 and Shared Project came up a lot but I don't understand how it is different to using a Class Library or a Portable Class Library.我正在查看 Visual Studio 2015 和共享项目的新功能,但我不明白它与使用类库或可移植类库有何不同。 Can anyone explain?谁能解释一下?

Edit: Shared Project is a new feature in Visual Studio 2015 and is different to a Portable Class Library.编辑:共享项目是 Visual Studio 2015 中的一项新功能,与可移植类库不同。 I understand what a Portable Class Library is.我了解便携式类库是什么。 What I'm trying to understand is how a Shared Project differs to a Class Library.我想了解的是共享项目与类库有何不同。 See link below.请参阅下面的链接。

http://www.c-sharpcorner.com/UploadFile/7ca517/shared-project-an-impressive-features-of-visual-studio-201/ http://www.c-sharpcorner.com/UploadFile/7ca517/shared-project-an-impressive-features-of-visual-studio-201/

The difference between a shared project and a class library is that the latter is compiled and the unit of reuse is the assembly.共享项目和类库的区别在于后者是编译的,重用的单位是程序集。

Whereas with the former, the unit of reuse is the source code, and the shared code is incorporated into each assembly that references the shared project.而对于前者,重用单​​元是源代码,共享代码被合并到引用共享项目的每个程序集中。

This can be useful when you want to create separate assemblies that target specific platforms but still have code that should be shared.当您想要创建针对特定平台但仍具有应该共享的代码的单独程序集时,这会很有用。

See also here :另见此处

The shared project reference shows up under the References node in the Solution Explorer, but the code and assets in the shared project are treated as if they were files linked into the main project.共享项目引用显示在解决方案资源管理器中的引用节点下,但共享项目中的代码和资产被视为链接到主项目的文件。


In previous versions of Visual Studio 1 , you could share source code between projects by Add -> Existing Item and then choosing to Link.在早期版本的 Visual Studio 1 中,您可以通过添加 -> 现有项目然后选择链接在项目之间共享源代码。 But this was kind of clunky and each separate source file had to be selected individually.但这有点笨拙,必须单独选择每个单独的源文件。 With the move to supporting multiple disparate platforms (iOS, Android, etc), they decided to make it easier to share source between projects by adding the concept of Shared Projects.随着支持多个不同平台(iOS、Android 等)的转变,他们决定通过添加共享项目的概念来更轻松地在项目之间共享源代码。


1 This question and my answer (up until now) suggest that Shared Projects was a new feature in Visual Studio 2015. In fact, they made their debut in Visual Studio 2013 Update 2 1这个问题和我的回答(到目前为止)表明共享项目是 Visual Studio 2015 中的一个新功能。事实上,它们在Visual Studio 2013 更新 2 中首次亮相

I found some more information from this blog .我从这个博客中找到了更多信息。

  • In a Class Library, when code is compiled, assemblies (dlls) are generated for each library.在类库中,编译代码时,会为每个库生成程序集 (dll)。 But with Shared Project it will not contain any header information so when you have a Shared Project reference it will be compiled as part of the parent application.但是对于共享项目,它不会包含任何头信息,因此当您有共享项目引用时,它将被编译为父应用程序的一部分。 There will not be separate dlls created.不会创建单独的 dll。
  • In class library you are only allowed to write C# code while shared project can have any thing like C# code files, XAML files or JavaScript files etc.在类库中,您只能编写 C# 代码,而共享项目可以包含任何内容,例如 C# 代码文件、XAML 文件或 JavaScript 文件等。

In-Short Differences are短期差异是

1) PCL is not going to have Full Access to .NET Framework , where as SharedProject has. 1) PCL 不会像 SharedProject 那样拥有对 .NET Framework 的完全访问权限。

2) #ifdef for platform specific code - you can not write in PCL ( #ifdef option isn't available to you in a PCL because it's compiled separately, as its own DLL, so at compile time (when the #ifdef is evaluated) it doesn't know what platform it will be part of. ) where as Shared project you can. 2) #ifdef 用于平台特定代码 - 您不能用 PCL 编写( #ifdef 选项在 PCL中不可用,因为它是单独编译的,作为它自己的 DLL,所以在编译时(当 #ifdef 被评估时)它不知道它将成为哪个平台的一部分。 )作为共享项目,您可以。

3) Platform specific code is achieved using Inversion Of Control in PCL , where as using #ifdef statements you can achieve the same in Shared Project. 3) 平台特定代码是在 PCL 中使用 Inversion Of Control 实现的,而在使用 #ifdef 语句时,您可以在共享项目中实现相同的功能。

An excellent article which illustrates differences between PCL vs Shared Project can be found at the following link可以在以下链接中找到说明 PCL 与共享项目之间差异的优秀文章

http://hotkrossbits.com/2015/05/03/xamarin-forms-pcl-vs-shared-project/ http://hotkrossbits.com/2015/05/03/xamarin-forms-pcl-vs-shared-project/

Like others already wrote, in short:就像其他人已经写过的一样,简而言之:

shared project共享项目
reuse on the code (file) level, allowing for folder structure and resources as well在代码(文件)级别重用,也允许文件夹结构和资源

pcl个人电脑
reuse on the assembly level在装配级重用

What was mostly missing from answers here for me is the info on reduced functionality available in a PCL: as an example you have limited file operations (I was missing a lot of File.IO fuctionality in a Xamarin cross-platform project).对我来说,这里的答案中最缺少的是有关 PCL 中可用功能减少的信息:例如,您的文件操作有限(我在 Xamarin 跨平台项目中缺少很多 File.IO 功能)。

In more detail更详细
shared project :共享项目
+ Can use #if when targeting multiple platforms (eg Xamarin iOS, Android, WinPhone) + 可以在针对多个平台(例如 Xamarin iOS、Android、WinPhone)时使用 #if
+ All framework functionality available for each target project (though has to be conditionally compiled) + 每个目标项目可用的所有框架功能(尽管必须有条件地编译)
o Integrates at compile time o 在编译时集成
- Slightly larger size of resulting assemblies - 所得组件的尺寸稍大
- Needs Visual Studio 2013 Update 2 or higher - 需要 Visual Studio 2013 Update 2 或更高版本

pcl :个人电脑
+ generates a shared assembly + 生成共享程序集
+ usable with older versions of Visual Studio (pre-2013 Update 2) + 可用于旧版本的 Visual Studio(2013 年更新 2 之前)
o dynamically linked o 动态链接
- lmited functionality (subset of all projects it is being referenced by) - 有限的功能(它被引用的所有项目的子集)

If you have the choice, I would recommend going for shared project, it is generally more flexible and more powerful.如果你有选择,我建议你去共享项目,它通常更灵活,更强大。 If you know your requirements in advance and a PCL can fulfill them, you might go that route as well.如果您提前知道您的要求并且 PCL 可以满足这些要求,那么您也可以走这条路。 PCL also enforces clearer separation by not allowing you to write platform-specific code (which might not be a good choice to be put into a shared assembly in the first place). PCL 还通过不允许您编写特定于平台的代码(首先将其放入共享程序集可能不是一个好的选择)来强制执行更清晰的分离。

Main focus of both is when you target multiple platforms, else you would normally use just an ordinary library/dll project.两者的主要焦点是当您针对多个平台时,否则您通常只会使用普通的库/dll 项目。

From the book VS 2015 succintly摘自书VS 2015

Shared Projects allows sharing code, assets, and resources across multiple project types.共享项目允许跨多个项目类型共享代码、资产和资源。 More specifically, the following project types can reference and consume shared projects:更具体地说,以下项目类型可以引用和使用共享项目:

  • Console, Windows Forms, and Windows Presentation Foundation.控制台、Windows 窗体和 Windows Presentation Foundation。
  • Windows Store 8.1 apps and Windows Phone 8.1 apps. Windows 应用商店 8.1 应用程序和 Windows Phone 8.1 应用程序。
  • Windows Phone 8.0/8.1 Silverlight apps. Windows Phone 8.0/8.1 Silverlight 应用程序。
  • Portable Class Libraries.便携式类库。

Note:- Both shared projects and portable class libraries (PCL) allow sharing code, XAML resources, and assets, but of course there are some differences that might be summarized as follows.注意:- 共享项目和可移植类库 (PCL) 都允许共享代码、XAML 资源和资产,但当然存在一些差异,可以总结如下。

  • A shared project does not produce a reusable assembly, so it can only be consumed from within the solution.共享项目不会产生可重用的程序集,因此只能在解决方案中使用它。
  • A shared project has support for platform-specific code, because it supports environment variables such as WINDOWS_PHONE_APP and WINDOWS_APP that you can use to detect which platform your code is running on.共享项目支持特定于平台的代码,因为它支持诸如 WINDOWS_PHONE_APP 和 WINDOWS_APP 之类的环境变量,您可以使用它们来检测您的代码在哪个平台上运行。
  • Finally, shared projects cannot have dependencies on third-party libraries.最后,共享项目不能依赖第三方库。
  • By comparison, a PCL produces a reusable .dll library and can have dependencies on third-party libraries, but it does not support platform environment variables相比之下,PCL 生成可重用的 .dll 库并且可以依赖第三方库,但它不支持平台环境变量

Class library is shared compiled code.类库是共享的编译代码。

Shared project is shared source code.共享项目是共享源代码。

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

相关问题 如何在 Visual Studio 代码中的多个项目之间包含共享的 class - how to include a shared class between several project in visual studio code Visual Studio 2015上的类库引用 - Class library references on Visual Studio 2015 Visual Studio Package和VSIX Project类型有什么区别? - What is the difference between Visual Studio Package and VSIX Project types? 在Visual Studio中//和///有什么区别? - What is the difference between // and /// in Visual Studio? Visual Studio 2015 Express上不提供类库和控制台应用程序项目模板 - Class Library and Console Application project templates not available on Visual Studio 2015 Express 在 Visual Studio 2015 for .net Core Class Library Project 中看不到模板 - Can't see templates in Visual Studio 2015 for .net Core Class Library Project 为什么我不能在 Visual Studio 2015 中创建共享项目? - Why can't I create Shared Project in Visual Studio 2015? 新的 Xamarin 共享项目创建在 Visual Studio 2015 上出现错误 - New Xamarin Shared Project creation gives errors on Visual Studio 2015 Visual Studio 2015 - Web项目中缺少共享项目参考选项卡 - Visual Studio 2015 - Shared Projects Reference Tab Missing on Web Project 如何在.NET核心类库中引用Visual Studio共享项目 - How do I reference a Visual Studio Shared Project in a .NET Core Class Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM