- | rssFeed | My book on MSBuild and Team Build | Archives and Categories Sunday, 07 March 2010

MSBuild 4.0: Property Functions Part 1

I have previously blogged about some new features in MSBuild 4.0 at:

Besides inline tasks there are a set of other new features including Property Functions. In this post we will discuss property functions and how you might use them in your build scripts. With property functions you can call an instance method of the string object on properties now.

The syntax will be in the format $(PropertyName.MethodName([Parameters])) when you want to invoke a string method. Parameters in the previous expression is optional. For instance if you need to call the Trim method then you do not need to supply any arguments. Take a look at the snippet below to get a better feel for how to use these new features.

<Project ToolsVersion="4.0" 
DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <SampleString>This is a sample string</SampleString> <SampleString2> This is a sample string </SampleString2> <!-- Here we now can call instance methods on the String class --> <Sub04>$(SampleString.Substring(0,4))</Sub04> <Contains01>$(SampleString.Contains("This"))</Contains01> <Contains02>$(SampleString.Contains("this"))</Contains02> <CompareTo01>$(SampleString.CompareTo($(SampleString)))</CompareTo01> <EndsWith01>$(SampleString.CompareTo("string"))</EndsWith01> <Insert01>$(SampleString.Insert(2,"INSERTED"))</Insert01> <Trim01>$(SampleString2.Trim())</Trim01> </PropertyGroup> <Target Name="Demo"> <Message Text="SampleString: $(SampleString)" Importance="high"/> <Message Text="Sub04: $(Sub04)" Importance="high"/> <Message Text="Contains01: $(Contains01)" Importance="high"/> <Message Text="Contains02: $(Contains02)" Importance="high"/> <Message Text="CompareTo01: $(CompareTo01)" Importance="high"/> <Message Text="EndsWith01: $(EndsWith01)" Importance="high"/> <Message Text="Insert01: $(Insert01)" Importance="high"/> <Message Text="Trim01: $(Trim01)" Importance="high"/> </Target> </Project>

In this snippet I am calling various string methods on the properties defined. The results of executing this are shown in the fragment below.

  SampleString: This is a sample string
  Sub04: This
  Contains01: True
  Contains02: False
  CompareTo01: 0
  EndsWith01: 1
  Insert01: ThINSERTEDis is a sample string
  Trim01: This is a sample string

This is just one way that you can use property functions. We can also call a static methods (and properties) on a known set of classes. See http://msdn.microsoft.com/en-us/library/dd633440%28VS.100%29.aspx for the list of complete classes that you can call static methods on. The syntax is as follows $([Full-Class-Name]::Method(Parameters)) or if you are calling a property you just leave off the (Parameters). To demonstrate this I have created the following file.

    DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

      Here we Now01 can call static methods/properties on many types 
      See http://msdn.microsoft.com/en-us/library/dd633440%28VS.100%29.aspx
      for the list of classes that we can call static methods/properties on.
  <Target Name="Demo">
    <Message Text="Now01: $(Now01)" Importance="high"/>
    <Message Text="Pow01: $(Pow01)" Importance="high"/>
    <Message Text="TempFile01: $(TempFile01)" Importance="high"/>


And the result is:

  Now01: 3/6/2010 7:31:23 PM
  Pow01: 8
  TempFile01: C:\Users\Ibrahim\AppData\Local\Temp\tmp4C1.tmp

Similar to this you can call a handful of MSBuild methods. Those methods are documented at http://msdn.microsoft.com/en-us/library/dd633440%28VS.100%29.aspx. You would use a similar syntax to access those which is $([MSBuild]::Method(Parameters)). To show you those take a look at the follwing sample file.


  <Target Name="Demo">
    <Message Text="Add: $(Add)" Importance="high"/>
    <Message Text="Subtract01: $(Subtract01)" Importance="high"/>
    <Message Text="Mult01: $(Mult01)" Importance="high"/>
    <Message Text="Div01: $(Div01)" Importance="high"/>


I will leave it up to you to execute that file to see the result, but you can probably figure it out :)

Sayed Ibrahim Hashimi

msbuild | MSBuild 4.0 Sunday, 07 March 2010 00:35:29 (GMT Standard Time, UTC+00:00)  #     |