简体   繁体   English

C#代码段文字无法正确扩展

[英]C# Code Snippet literal not expanding correctly

I'm facing a strange issue trying to build a custom code snippet around a simple Guard class that does the argument checks. 我在尝试围绕执行参数检查的简单Guard类构建自定义代码段时遇到一个奇怪的问题。

Guard Class 警卫班

public sealed class Guard
{
    public static void Null(string paramName, object value)
    {
        if (value == null) throw new ArgumentNullException(paramName);
    }
}

Code Snippet 代码段

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
<Header>
  <Title>Guard Guid Check</Title>
  <Author>Chris Richner</Author>
  <Description></Description>
  <HelpUrl></HelpUrl>
  <SnippetTypes />
  <Keywords />
  <Shortcut>gg</Shortcut>
</Header>
<Snippet>
  <References />
  <Imports>
    <Import>
      <Namespace>Foundation</Namespace>
    </Import>
  </Imports>
  <Declarations>
    <Literal Editable="true">
      <ID>argument</ID>
      <Type>System.Guid</Type>
      <ToolTip />
      <Default></Default>
      <Function />
    </Literal>
  </Declarations>
  <Code Language="csharp" Kind="" Delimiter="$"><![CDATA[Guard.Guid(nameof($argument$), $argument$);$selected$$end$]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Should generate the following code 应该生成以下代码

Guard.Null(nameof(arg), arg);

But this is not working because only the first argument$ literal is highlighted in Visual Studio 2015 RTM Code Editor and finally filled with the given argument name. 但这不起作用,因为在Visual Studio 2015 RTM代码编辑器中仅突出显示第一个arguments $文字,并最终用给定的参数名称填充。

What do I miss? 我想念什么? Thanks! 谢谢!

Try the following. 尝试以下方法。

<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Guard Guid Check</Title>
    <Author>Chris Richner</Author>
    <Shortcut>gg</Shortcut>
    <Description></Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
      <SnippetType>SurroundsWith</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>argument</ID>
        <ToolTip>Enter argument name</ToolTip>
        <Default>argument</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp"><![CDATA[
    Guard.Null(nameof($argument$), $argument$);
    ]]></Code>
  </Snippet>
</CodeSnippet>

Looking through your code there are some things that I dont recognize. 查看您的代码,有些事情我不认识。 1) I am not sure what the $selected$$end$ are for and 2) I am not sure if the language property (csharp) is case sensitive. 1)我不确定$ selected $$ end $的用途是什么,以及2)我不确定language属性(csharp)是否区分大小写。

I took your information and placed it in a snippet that I know works correctly from VS 2005 to VS 2013. Then I tested the snippet and it worked correctly for me. 我获取了您的信息,并将其放入了一段我知道可以在VS 2005到VS 2013中正常工作的代码段中。然后,我对该代码段进行了测试,它对我来说正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM