【问题标题】:Get wildcard reference in csproj node在 csproj 节点中获取通配符引用
【发布时间】:2021-06-16 12:15:31
【问题描述】:

举个例子:

<EmbeddedResource Include="Resources\files\main-*.png">
   <LogicalName>Images\main\???.resources</LogicalName>
</EmbeddedResource>

如何从节点的子节点中的“*”获取部分?我找到了 %Filename%,但这太多了。

【问题讨论】:

    标签: msbuild csproj


    【解决方案1】:

    有可能,但需要一些property functions功夫。

    <Project>
        <!--
            Create files:
            Resources\files\main-foo.png
            Resources\files\main-bar.png
    
            Run:
            MSBuild.exe foo.proj /t:Test
    
            Output:
            LogicalName: Images\main\foo.resources
            LogicalName: Images\main\bar.resources
        -->
    
        <ItemGroup>
            <EmbeddedResource Include="Resources\files\main-*.png">
                <_DashPosition>$([System.String]::Copy('%(FileName)').IndexOf('-'))</_DashPosition>
                <_LogicalNameStart>$([MSBuild]::Add(%(_DashPosition), 1))</_LogicalNameStart>
                <LogicalName>Images\main\$([System.String]::Copy('%(FileName)').Substring(%(_LogicalNameStart))).resources</LogicalName>
            </EmbeddedResource>
        </ItemGroup>
    
        <Target Name="Test">
            <Message Text="LogicalName: %(EmbeddedResource.LogicalName)" Importance="high"/>
        </Target>
    
    </Project>
    

    一般方法就像使用 C# 或其他语言一样。找到分隔符(此处为文件名中的-)并将其后的所有内容作为LogicalName。

    请注意,我使用了“临时”项目元数据(“_”在这里只是一个约定)以使事情更加清晰。从理论上讲,您可以将所有内容都塞进&lt;LogicalName&gt; 本身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      相关资源
      最近更新 更多