Visual Studio项目/项模板参数逻辑

时间:2011-07-15 15:02:25

标签: visual-studio logic project-template

由于我只看了几篇关于这个主题的帖子,但没有深入解释Visual Studio模板中参数的逻辑,我想我会在这里发布。

MSDN article之后,您可以向模板添加自定义参数,如果您想要更改这些参数,可以使用Wizard进行更改。

在模板的任何文件中(模板文件本身除外),您可以根据参数添加逻辑。逻辑只使用三个关键字。 $ if $(%expression%),$ else $和$ endif $。所以说我在模板文件中有以下内容:

public string foo( string a )
{
    return string.Format( @"foo( {0} );", a );
}

我们想要添加一些逻辑,以确定我们是否要检查“a”是空还是空

public string foo( string a )
{
$if$ ( $shouldCheckForNullOrEmpty$ == true )
    if ( !string.IsNullOrEmpty( a ) )
$endif$

    return string.Format( @"foo( {0} );", a );
}

当然,您可能希望为if语句添加大括号,因此您可能需要多个逻辑块。

所以这不是太糟糕,但这有一些技巧。 $ if $检查字符串匹配,即shouldCheckForNullOrEmpty必须等于“true”。它也被写成$ if $($ shouldCheckForNullOrEmpty $ ==“true”),但那不起作用。

使用单个表达式的单个if语句非常简单,现在对于更复杂的示例:

public string foo( string a )
{
$if$ ( $parameterCheckMode$ == ifNullOrEmpty )
    if ( !string.IsNullOrEmpty( a ) )
$else$ $if$ ( $parameterCheckMode$ == throwIfNullOrEmpty )
    if ( string.IsNullOrEmpty( a ) )
        throw new ArgumentException();
$endif$ $endif$

    return string.Format( @"foo( {0} );", a );
}

正如您可能知道的那样,这是参数模式的switch语句。您可能会注意到没有$ elseif $所以你必须使$ else $ $ if $但是必须在最后添加额外的$ endif $。

最后,我还没有找到逻辑的符号。我只是使用逻辑等价来解决这个问题:

和 - > $ if $(expression1)$ if $(expression2)$ endif $ endif $

或 - > $ if $(expression1)语句$ else $ $ if $ statement $ endif $ $ endif $

希望这有助于某人。

1 个答案:

答案 0 :(得分:0)

对于逻辑'和'和'或' '和'是:
&安培;&安培;
和'或'是:
||
所以带有'和'的if语句看起来像这样:

if ((a != null)&&(a !=""))
{
    Console.Write(a);
}

带有'或'的if语句看起来像这样:

if ((b != null)||(b >= 5))
{
    Console.Write(b);
}

对于模板,您可以将* .cs文件导出为模板。它位于Project> Export Template ...

(我使用的是VisualStudios 2017)

希望这会有所帮助。

相关问题