Visual Studio Project/Item Template Parameter Logic

This post is linked from the official Microsoft documentation for MSBuild Conditions as having examples of usage. The syntax for MSBuild conditions is not exactly equivalent to the way templating $if$ statements work but it’s a close approximation. However, it seems the examples here are now obsolete or were never completely accurate to begin with. After some extensive testing I have found the following to be true in regards to templating $if$ statements:

$if$ statements can never be nested. The reason for this is the way in which the templating engine handles the case of an $if$ statement evaluating to false. When an $if$ expression evaluates to false the engine will parse until the next $endif$ it finds and remove everything in between. This means when there is a nested $if$ statement, that will be the first $endif$ that is reached and the nested $if$ expression will be removed before processing. The same is true when using $else$. There are a few misleading examples in the “question” above that will only work in the case that the outer $if$ statement evaluates to true. Consider the following example where the outer $if$ statement does not evaluate to true:

public void foo()
{
    string str = "";

    $if$ ($true$ == false)
    str = "foo";

    $if$ ($true$ == true)
    str = str + "bar";
    $endif$ //inner

    $endif$ //outer

    return str;
}

The result of running this template would be the following:

public void foo()
{
    string str = "";

    //inner

    $endif$ //outer

    return str;
}

As you can see, everything after the first $endif$ is left in the code. I’ve come up with a suitable workaround that involves using a combination of the & operator and serial if statements.

There is a logical & operator that can be used within a single if statement. The syntax is as follows:

$if$ ($foo$|$bar$ == true|false)

This will evaluate as $foo$ == true && $bar$ == false. It is valid to use more than two operands, e.g. $foo$|$bar$|$baz$|... == true|false|etc|.... The caveat to this usage is that you can only use a single operator type for all comparisons in the statement (==|!= is not valid). I was unable to find anything comparable to an Or operator.

Putting it all together. The & operator can be used with a series of if statements to create a simulated nested-if environment. The following example shows 3 levels of nesting if(foo){...if(bar){...if(baz){...}...}...}:

public void foo()
{
    string str = "";

    $if$ ($ext_foo$ == true)
    str = "foo";
    $endif$$if$ ($ext_foo$|$ext_bar$ == true|true)
    str = str + "bar";
    $endif$$if$ ($ext_foo$|$ext_bar$|$ext_baz$ == true|true|true)
    str = str + "baz";
    $endif$$if$ ($ext_foo$|$ext_bar$ == true|true)
    str = str + "bar";
    $endif$$if$ ($ext_foo$ == true)
    str = str + "foo";
    $endif$

    return str;
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)