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.

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

  
    This is a sample string
       This is a sample string   
    
    $(SampleString.Substring(0,4))
    $(SampleString.Contains("This"))
    $(SampleString.Contains("this"))
    $(SampleString.CompareTo($(SampleString)))
    $(SampleString.CompareTo("string"))
    $(SampleString.Insert(2,"INSERTED"))
    $(SampleString2.Trim())
  
  
  
    
    
    
    
    
    
    
    
  

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

Demo:
  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.



  
    
    $([System.DateTime]::Now)
    $([System.Math]::Pow(2,3))
    $([System.IO.Path]::GetTempFileName())
  
  
  
    
    
    
  

And the result is:

Demo:
  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.



  
    $([MSBuild]::Add(5,9))
    $([MSBuild]::Subtract(90,768))
    $([MSBuild]::Multiply(4,9))
    $([MSBuild]::Divide(100,5.2))
  
  
  
    
    
    
    
  

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


Comment Section

Comments are closed.