问题描述
我在VS2015中有两个项目
I have two projects in VS2015
- 一个.NET Core(project.json,xproj)
- 一个常规的.NET csproj
当我使用VS2017打开项目1时,它很好地迁移到了新的csproj格式.
When I open project 1 with VS2017, it nicely migrates to the new csproj format.
Project 2在VS2017中工作,但我想将此csproj转换/迁移/更新为新的项目文件格式,以从csproj的新功能(多目标,无大文件列表,可读的csproj,csproj中的NuSpec信息等)中受益)
Project 2 works in VS2017, but I like to convert/migrate/update this csproj to the new project file format to benefit from the new csproj features (multi target, no large file list, readable csproj, NuSpec info in csproj etc)
我该怎么做?我无法在VS2017中找到该选项.
How could I do that? I cannot find an option in VS2017 for that.
使用:VS2017 RTM
Using: VS2017 RTM
推荐答案
VS中没有内置选项,因为更清洁的csproj主要用于.NET Core项目.没有在项目文件中花费一些精力和进一步的自定义,它就不能完全与所有其他项目类型一起使用.例如,对于新的SDK样式(新样式).csproj,连接代码生成器或在Winforms或WPF应用程序中进行嵌套之类的操作可能无法立即使用.通常可以通过根据需要更新元数据来解决这些问题:
There isn't an option built into VS because the cleaner csproj is primarily for .NET Core projects. It does not fully work with all other project types without some effort and futher customizations in the project file. For example, wiring up things like code generators or nesting in Winforms or WPF apps may not work out of the box for a new SDK-style (new style) .csproj. These can usually be worked around by updating metadata as needed:
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
请注意,这里的Update属性是关键-您不想再次在ItemGroup中包含文件,而只想更新其现有(或缺少)的元数据.
Note that the Update attribute is key here - you don't want to include the file a second time in the ItemGroup, you just want to update its existing (or lacking) metadata.
对于某些项目类型也有一些警告,在基于SDK的csproj vs和旧版本中,目标没有按预期导入.这取决于目标的编写方式,但是其中一个示例是,在基于SDK的项目中使用VSSDK构建VSIX项目(VS扩展)确实很难.
There are also some caveats for some project types where targets do not get imported as expected in an SDK-based csproj vs and old one. This depends on how your targets are authored, but one example of this is building VSIX projects (VS extensions) using the VSSDK is really hard to get right in an SDK-based project.
如果您想尝试转换,请内特·麦克马斯特(Nate McMaster)博客文章是很好的资源.他介绍了从头开始和就地转换文件的过程.我发现大多数情况下,从新的干净的.csproj开始并添加所需的任何解决方法都非常顺利.
If you want to try the conversion, Nate McMaster's blog post is an excellent resource. He covers both starting from scratch and converting the file in situ. I've found that most of the time it goes pretty smoothly to start with a new clean .csproj and add in any workaround needed.
值得指出的是,普通csproj文件中的很多废话只是生成的内容,而进行一点点手工编辑就可以使清理工作大有帮助.例如,使用浮动模式可以在大型项目文件中保存数百行.这是基于SDK的项目无论如何工作的一部分-它们只是在SDK中进行处理,因此它不会显示在.csproj中,但工作原理几乎相同.例如,如果要(递归地)将所有C#文件包括在一个文件夹下,则可以执行以下操作:
It's also worth pointing out that a lot of the crap in your normal csproj file is just generated content, and a little hand-editing can go a long way towards cleaning it up. Using globbing patterns, for example, can save hundreds of lines in a large project file. This is part of how SDK-based projects work anyways - they just do the globs in the SDK, so it doesn't show up in the .csproj, but it works nearly the same. For example, if you want to include all the C# files under a folder (recursively), you can do something like this:
<ItemGroup>
<!-- Include all C# files under the SampleFolder and any child folders -->
<Compile Include="SampleFolder\**\*.cs" />
<!-- Include all C# files under this project's root: -->
<Compile Include="**\*.cs" />
<ItemGroup>
**
与任何递归文件夹路径匹配.
The **
matches any recursive folder path.
Visual Studio行为存在一些差异,因为用于基于SDK的项目的新项目系统的工作方式与旧的.csproj项目系统非常不同.例如,SDK项目将自动检测磁盘上文件的更改时间,并在项目中添加/删除文件.在重新加载项目之前,old-csproj不会在VS中更新它们.
There are some differences in Visual Studio behavior, because the new project system for SDK-based projects works very different than the old .csproj project system. For example, SDK-projects will automatically detect when files change on disk and add/remove them in the projects; old-csproj won't update them in VS until the project is reloaded.
如果确实在所有地方都使用了遍历,则会遇到与基于SDK的csproj类似的问题,其中某些文件未连接到其代码生成器或未正确嵌套.如上所述,您可以使用Update=
属性修正特定文件(甚至更特定的glob),以使元数据恢复原样.
If you do use globbing everywhere, you'll run into a similar problem as in SDK-based csproj, where certain files are not hooked up to their code generators or nested correctly. As above, you can use the Update=
attribute to fix up specific files (or even more specific globs) to get the metadata back to the same.
这篇关于如何将非核心的csproj转换为VS2017格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!