代码之家  ›  专栏  ›  技术社区  ›  Sergej Andrejev

MSBuild在下划线符号之前获取属性子字符串

  •  13
  • Sergej Andrejev  · 技术社区  · 14 年前

    3 回复  |  直到 14 年前
        1
  •  38
  •   kkm -still wary of SE promises    7 年前

    使用MSBuild 4

    property functions .

    <PropertyGroup>
      <MyProperty>Name_Something</MyProperty>
    </PropertyGroup>
    
    <Target Name="SubString">
      <PropertyGroup>
        <PropertyName>$(MyProperty.Substring(0, $(MyProperty.IndexOf('_'))))</PropertyName>
      </PropertyGroup>
    
      <Message Text="PropertyName: $(PropertyName)"/>
    </Target>
    

    带MSBuild<4

    你可以用这个 RegexReplace 的任务 MSBuild Community Task

    <PropertyGroup>
      <MyProperty>Name_Something</MyProperty>
    </PropertyGroup>
    
    <Target Name="RegexReplace">
      <RegexReplace Input="$(MyProperty)" Expression="_.*" Replacement="" Count="1">
        <Output ItemName ="PropertyNameRegex" TaskParameter="Output" />
      </RegexReplace>
    
      <Message Text="PropertyNameRegex: @(PropertyNameRegex)"/>
    </Target>
    
        2
  •  -3
  •   cHao    14 年前

    如果我正确理解您的问题,那么您正在尝试获取MSBuild属性的子字符串。在MSBuild中没有直接的方法来进行字符串操作,就像在NAnt中一样。所以你有两个选择:

    1). 为每个零件创建单独的变量并将其组合:

    <PropertyGroup>
      <Name>Name</Name>
      <Something>Something</Something>
      <Combined>$(Name)_$(Something)</Combined>
    </PropertyGroup>
    

    如果零件是在手之前就知道的,那么这个方法可以很好地工作,但是如果您需要动态地这样做的话,这个方法就不行了。

    2). 编写执行字符串操作的customer MSBuild任务。如果需要在运行时完成,这将是您唯一的选择。

        3
  •  -4
  •   Filburt kukabuka    14 年前

    看起来你需要 而不是属性:

    <ItemGroup>
        <Something Include="SomeValue">
            <Name>YourName</Name>
            <SecondName>Foo</SecondName>
        </Something>
    </ItemGroup>