Anton Gogolev
Anton Gogolev

Reputation: 115691

Nullable parameter values in MSBuild

I'm writing an MSBuild task to upgrade a database (full source here) and encountered an error/by design feature I don't know how to deal with. Basically, if I declare:

public int? TargetVersion
{
    [DebuggerStepThrough]
    get { return targetVersion; }
    [DebuggerStepThrough]
    set { targetVersion = value; }
}

and then attempt to assign a value in an .msbuild file:

<Target Name="Upgrade">
    <UpgradeDatabase ... TargetVersion="10" />
</Target>

MSBuild freaks out and says that

error MSB4030: "10" is an invalid value for the "TargetVersion" parameter of the "UpgradeDatabase" task. The "TargetVersion" parameter is of type "System.Nullable`1[System.Int32]".

How do I assign a value to a nullable property?

Upvotes: 3

Views: 1368

Answers (2)

Vaccano
Vaccano

Reputation: 82291

I would suggest you look into the [Required] tag a bit more. That is how MSBuild handles optional vs required parameters.

Upvotes: 2

OregonGhost
OregonGhost

Reputation: 23759

MSBuild doesn't seem to support nullable values then. A workaround would be to use the nullable property internally, but provide a public non-nullable property. This way, the first assignment to the public property will set the internal value from null to a real value, so you have null in a freshly initialized instance, but MSBuild can happily assign its values.

That is, unless there is some way to trick MSBuild into supporting nullables directly :)

Upvotes: 2

Related Questions