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. Can anyone explain?
我正在研究Visual Studio 2015的新功能,共享项目经常出现,但我不理解使用类库或可移植类库的方式有什么不同。谁能解释?
Edit: Shared Project is a new feature in Visual Studio 2015 and is different to a Portable Class Library. 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.
编辑:共享项目是Visual Studio 2015的一个新特性,不同于可移植类库。我知道什么是可移植类库。我试图理解的是共享项目与类库的区别。请参阅下面的链接。
http://www.c-sharpcorner.com/UploadFile/7ca517/shared-project-an-impressive-features-of-visual-studio-201/
5 个解决方案
#1
192
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.
共享项目引用显示在解决方案资源管理器的References节点下,但是共享项目中的代码和资产被视为链接到主项目的文件。
In previous versions of Visual Studio1, you could shared source code between projects by Add -> Existing Item and then choosing to Link. 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.
在Visual Studio1的以前版本中,您可以通过添加->现有项目在项目之间共享源代码,然后选择链接。但这有点笨拙,每个单独的源文件都必须单独选择。为了支持多个不同的平台(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
这个问题和我的答案(到目前为止)表明共享项目是Visual Studio 2015的一个新特性。事实上,他们在Visual Studio 2013 Update 2中首次亮相
#2
27
I found some more information from this blog.
我从这个博客中找到了更多的信息。
- In a Class Library, when code is compiled, assemblies (dlls) are generated for each library. 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)。但是,在共享项目中,它将不包含任何标题信息,因此当您有一个共享的项目引用时,它将作为父应用程序的一部分被编译。不会创建单独的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文件等。
#3
16
In-Short Differences are
简而言之差异
1) PCL is not going to have Full Access to .NET Framework , where as SharedProject has.
1) PCL将不能完全访问。net框架,而SharedProject拥有。net框架。
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中的反转控制实现的,其中使用#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/
#4
11
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
在组装层上重用pcl
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跨平台项目中的IO fuc合度)。
In more detail
shared project:
+ Can use #if when targeting multiple platforms (e. g. Xamarin iOS, Android, WinPhone)
+ All framework functionality available for each target project (though has to be conditionally compiled)
o Integrates at compile time
- Slightly larger size of resulting assemblies
- Needs Visual Studio 2013 Update 2 or higher
详细共享项目:+可以使用#如果针对多个平台(e . g。Xamarin的iOS、Android,WinPhone)+所有可用功能框架为每个目标项目(尽管是条件编译)o集成在编译时-略大生成的程序集的大小需要Visual Studio 2013更新2或更高
pcl:
+ generates a shared assembly
+ usable with older versions of Visual Studio (pre-2013 Update 2)
o dynamically linked
- lmited functionality (subset of all projects it is being referenced by)
pcl: +生成一个共享程序集+可与Visual Studio的旧版本(2013年之前的更新2)o动态链接- lmited功能(它被引用的所有项目的子集)一起使用
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 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可以满足它们,那么您也可以走这条路。PCL还通过不允许编写特定于平台的代码来实现更清晰的分离(这可能不是将其放入共享程序集的最佳选择)。
Main focus of both is when you target multiple platforms, else you would normally use just an ordinary library/dll project.
两者的主要关注点都是当您针对多个平台时,否则您通常只使用一个普通的库/dll项目。
#5
7
From the book VS 2015 succintly
从《2015年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表示基础。
- Windows Store 8.1 apps and Windows Phone 8.1 apps.
- Windows Store 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库,并且可以依赖于第三方库,但是它不支持平台环境变量
#1
192
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.
共享项目引用显示在解决方案资源管理器的References节点下,但是共享项目中的代码和资产被视为链接到主项目的文件。
In previous versions of Visual Studio1, you could shared source code between projects by Add -> Existing Item and then choosing to Link. 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.
在Visual Studio1的以前版本中,您可以通过添加->现有项目在项目之间共享源代码,然后选择链接。但这有点笨拙,每个单独的源文件都必须单独选择。为了支持多个不同的平台(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
这个问题和我的答案(到目前为止)表明共享项目是Visual Studio 2015的一个新特性。事实上,他们在Visual Studio 2013 Update 2中首次亮相
#2
27
I found some more information from this blog.
我从这个博客中找到了更多的信息。
- In a Class Library, when code is compiled, assemblies (dlls) are generated for each library. 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)。但是,在共享项目中,它将不包含任何标题信息,因此当您有一个共享的项目引用时,它将作为父应用程序的一部分被编译。不会创建单独的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文件等。
#3
16
In-Short Differences are
简而言之差异
1) PCL is not going to have Full Access to .NET Framework , where as SharedProject has.
1) PCL将不能完全访问。net框架,而SharedProject拥有。net框架。
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中的反转控制实现的,其中使用#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/
#4
11
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
在组装层上重用pcl
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跨平台项目中的IO fuc合度)。
In more detail
shared project:
+ Can use #if when targeting multiple platforms (e. g. Xamarin iOS, Android, WinPhone)
+ All framework functionality available for each target project (though has to be conditionally compiled)
o Integrates at compile time
- Slightly larger size of resulting assemblies
- Needs Visual Studio 2013 Update 2 or higher
详细共享项目:+可以使用#如果针对多个平台(e . g。Xamarin的iOS、Android,WinPhone)+所有可用功能框架为每个目标项目(尽管是条件编译)o集成在编译时-略大生成的程序集的大小需要Visual Studio 2013更新2或更高
pcl:
+ generates a shared assembly
+ usable with older versions of Visual Studio (pre-2013 Update 2)
o dynamically linked
- lmited functionality (subset of all projects it is being referenced by)
pcl: +生成一个共享程序集+可与Visual Studio的旧版本(2013年之前的更新2)o动态链接- lmited功能(它被引用的所有项目的子集)一起使用
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 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可以满足它们,那么您也可以走这条路。PCL还通过不允许编写特定于平台的代码来实现更清晰的分离(这可能不是将其放入共享程序集的最佳选择)。
Main focus of both is when you target multiple platforms, else you would normally use just an ordinary library/dll project.
两者的主要关注点都是当您针对多个平台时,否则您通常只使用一个普通的库/dll项目。
#5
7
From the book VS 2015 succintly
从《2015年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表示基础。
- Windows Store 8.1 apps and Windows Phone 8.1 apps.
- Windows Store 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库,并且可以依赖于第三方库,但是它不支持平台环境变量