S h o r t S t o r i e s

// Tales from software development

Are write-only properties ever useful ?

leave a comment »

I’ve previously thought of write-only properties as a useless curiosity but in implementing an MSBuild task last week I finally found a use for them.

The build task I wrote updated the AssemblyFileVersion attribute value in a C# AssemblyInfo.cs file. The task implements properties to specify the path of the file to be updated and which part of the major.minor.build.revision style version number should be incremented. The task returns the updated version value as an output parameter. The call to the task from an MSBuild project file looks like this:

    <Output TaskParameter=”UpdatedVersion” PropertyName=”VersionString” />

The implementation of the task requires the input and output parameters to be defined as properties that are then decorated with MSBuild related attributes such as Required and Output. For example the File property might be implemented as:

public string File
    get { return this.versionFilePath; }
    set { this.versionFilePath = value; }

The property set accessor is required so that the property value can be passed from the MSBuild project file to the task but is the property get accessor required ? In this example it isn’t because the task accesses the underlying private member value directly. The implementation of the property is actually the implementation of an interface to an external environment that only needs to pass a value to the task so this code is all that is required:

public string File
    set { this.versionFilePath = value; }

I’ve finally encountered a use for write-only properties!

Note that this isn’t always true of MSBuild task parameters. Although tasks typically implement input parameters and output parameters there is no reason why a parameter shouldn’t be defined as an input and output parameter.


Written by Sea Monkey

July 7, 2009 at 8:00 pm

Posted in Development

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: