Click here to Skip to main content
12,350,961 members (41,623 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked

MSBuild: Factorial!

, 20 Jun 2009 Public Domain
Rate this:
Please Sign up or sign in to vote.
Using MSBuild Targets as general-purpose functions, complete with parameters and return values.

I've been doing some exploring of MSBuild as a programming language. There are some interesting results regarding mutability/immutability, but that's for another post.

This post is about functions. In particular, a Target may be invoked using the MSBuild task, so I'm exploring using Targets as functions. MSBuild can pass parameters to a Target by sending it Properties. Property changes are not propogated back to the caller, though, so getting a return value is a bit trickier.

It turns out that MSBuild does return one bit of information from a Target: its Outputs. It's possible to set the Outputs of a Target to a Property, and have that Target depend on another Target that sets that Property. In this way, it is possible to create a pair of Targets that can "calculate" the outer Target's Outputs.

By combining these approaches (setting Properties for arguments, and using the Target's Outputs as a return value), it is possible to treat a Target as a function.

To demonstrate, I wrote this program, which uses MSBuild to recursively calculate the factorial of the $(Input) property. Have fun playing!

 <Project ToolsVersion="3.5" xmlns="<a href=""></a>">

msbuild factorial.proj /nologo /clp:v=minimal /p:Input=5

Result: 120

msbuild factorial.proj /nologo /clp:v=minimal /p:Input=7

Result: 5040

Useless, but cool nonetheless.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Stephen Cleary
Software Developer (Senior)
United States United States
Stephen Cleary is a Christian, husband, father, and programmer living in Northern Michigan.

Personal home page (including blog):

You may also be interested in...

Comments and Discussions

GeneralNovel! Pin
Trollslayer20-Jun-09 10:27
memberTrollslayer20-Jun-09 10:27 
GeneralRe: Novel! Pin
Stephen Cleary20-Jun-09 16:49
memberStephen Cleary20-Jun-09 16:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 20 Jun 2009
Article Copyright 2009 by Stephen Cleary
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid