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

mas*_*all 28 logic project-template visual-studio

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

MSDN文章之后,您可以向模板添加自定义参数,如果您想要更改它们,可以使用向导进行更改.

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

public string foo( string a )
{
    return string.Format( @"foo( {0} );", a );
}
Run Code Online (Sandbox Code Playgroud)

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

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

    return string.Format( @"foo( {0} );", a );
}
Run Code Online (Sandbox Code Playgroud)

当然,您可能希望为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 );
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

希望这有助于某人.

Har*_*y S 0

对于逻辑andand or
andis:
&&
while oris:
||

因此,带有 in 的 if 语句and将如下所示:

if ((a != null)&&(a !=""))
{
    Console.Write(a);
}
Run Code Online (Sandbox Code Playgroud)

其中的 if 语句or如下所示:

if ((b != null)||(b >= 5))
{
    Console.Write(b);
}
Run Code Online (Sandbox Code Playgroud)

对于模板,您可以导出 *.cs 文件作为模板。它位于项目>导出模板...

(我使用的是 VisualStudios 2017)

希望这有帮助。