Visual Studio 2017:在类库项目中嵌套文件 [英] Visual studio 2017 : nesting files in a class library project

查看:186
本文介绍了Visual Studio 2017:在类库项目中嵌套文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Web项目中,您可以选择嵌套文件

In web projects you have the option of nesting files

 + startup.cs
   +--startup.internals.cs
   +--startup.configuration.cs

在类库项目中还有什么方法可以实现相同的行为吗?

Is there any way to achieve the same behavior in a class library project as well ?

更新:已部分解决

确定,

您需要了解文件路径.

对于这样的结构(文件处于项目级别)

for a structure like this (the files are at project level)

+-- myProject.csproj
   +-- startup.cs
   +-- startup.internals.cs
   +-- startup.configuration.cs

这就是您要寻找的配置.

Then this is the configuration you are looking for.

  <ItemGroup>
    <Compile Update="startup.*.cs">
      <DependentUpon>startup.cs</DependentUpon>
    </Compile>
  </ItemGroup>

对于嵌套文件夹结构

+-- myProject.csproject
   +-- Folder_A
      +-- Folder_A1
         +-- startup.cs
         +-- startup.internals.cs
         +-- startup.configuration.cs

您需要使用buildin $ {ProjectDir)宏来掌握项目路径

you need to get hold of the project path using the buildin $(ProjectDir) macro

  <ItemGroup>
    <Compile Update=" $(ProjectDir)\Folder_A\Folder_A1\startup.*.cs">
      <DependentUpon> $(ProjectDir)\Folder_A\Folder_A1\startup.cs</DependentUpon>
    </Compile>
  </ItemGroup>

为什么要说它部分起作用,因为如果我退出Visual,然后再次打开它,对于第二种类型的结构,它将取消嵌套文件的操作.

Why do I say it is partially working, well because if I exit Visual and then open it again, for the 2nd type of structure, it will un-nest the files.

有人帮助吗?

推荐答案

要回答您的更新:部分解决:

类似于这个问题. DependentUpon是用于实现所需嵌套的正确元素.

Something similar to this issue. DependentUpon is the correct element to use to achieve the nesting that you want.

但是,您的值不能是包含通配符的表达式.它必须是位于同一文件夹或子文件夹下的单个文件.如果文件位于子文件夹中,则必须通过相对路径指定文件.因此,您的ItemGroup的内容应如下所示:

But, your value cannot be expression that includes wildcards. It must be a single file that is under the same folder or a sub-folder. If your file exists in a sub-folder, you must specify the file via a relative path. So the content of your ItemGroup should be like this:

<Compile Include="Folder_A\Folder_A1\startup.cs" />
<Compile Include="Folder_A\Folder_A1\startup.configuration.cs">
  <DependentUpon>startup.cs</DependentUpon>
</Compile>
<Compile Include="Folder_A\Folder_A1\startup.internals.cs">
  <DependentUpon>startup.cs</DependentUpon>
</Compile>

经过检查,我认为如果要构建.NET FX类库项目,则需要使用Include属性而不是Update.

And after my check, I think you need to use the Include attribute instead of Update if you are building a .NET FX class library project.

更新: Update属性仅根据此文档.

如果在.NET Core项目中(如您在注释中指出的那样),也许Update更合适:

And if in a .NET Core project, like what you indicated in a comment, maybe Update is more suitable:

<Compile Update="Folder_A\Folder_A1\startup.internals.cs">
  <DependentUpon>startup.cs</DependentUpon>
</Compile>

这篇关于Visual Studio 2017:在类库项目中嵌套文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆