[英]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.当您想要创建针对特定平台但仍具有应该共享的代码的单独程序集时,这会很有用。
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-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:更具体地说,以下项目类型可以引用和使用共享项目:
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 资源和资产,但当然存在一些差异,可以总结如下。
Class library is shared compiled code.类库是共享的编译代码。
Shared project is shared source code.共享项目是共享源代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.