特殊变量:
$end$:这是一个特殊参数,用于指定用户在代码段字段中填写完内容并按下 Enter 键后光标所在的位置。
$selected$:这是一个特殊参数,只对 SurroundsWith 类型的代码段有意义。
Function:
ClassName() 获取类名
SimpleTypeName(global::System.Exception) 获取异常信息
SimpleTypeName(global::System.EventHandler) 获取事件句柄
SimpleTypeName(TypeName) 在Snippet所在的上下文中推断出TypeName参数的最简单形式
GenerateSwitchCases($expression$) $expression$是Switch的条件表达式获取cases的值
常见变量的写法:
$type$ : 要循环访问的集合中对象的类型 例如: object
$identifier$: 要循环访问的集合中对象的类型
$collection$: 要循环访问的集合或数组的名称
$expression$: 要计算的预处理器表达式 默认值: true
$name$: 名称
$classname$(): 类的名称, Function:
$SystemAttributeUsage$: 特性的名称
$index$: 索引
$max$: 最大长度
$access$: 访问修饰符 例如: public
$indextype$ : 用于指定索引的类型 例如: int
$DelegateType$: 委托类型 默认值:EventHandler
$property$: 属性名
$field$: 支持此属性的变量
$cases$: 遍历循环表达式, Function: GenerateSwitchCases($expression$) $expression$是Switch的条件表达式
($expression$)
打开VS的代码管理器:工具->代码片段管理器,如下图所示:
代码片段管理器
Visual Studio默认已经给我们写了好多代码片段了,如果你觉得这些代码片段不够用,想自定义一个代码片段的话,步骤如下:
编辑代码片段
新建一个代码片段文件,```注:代码片段是xml文件,以.snippet为后缀名``,比如我自定义了一个代码片段,文件名是bpr.snippet,右击该文件,在编辑器中打开进行编辑:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>产品响应实体</Title>
<Shortcut>bpr</Shortcut>
<Description>获取产品响应实体的代码片段</Description>
<Author>心彻</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace></Namespace>
</Import>
</Imports>
<References>
<Reference>
<Assembly></Assembly>
</Reference>
</References>
<Declarations>
<Literal>
<ID>name</ID>
<ToolTip>销售一体化DSF调用</ToolTip>
<Default>MyTestMethod</Default>
</Literal>
<Literal Editable="false">
<ID>TestMethod</ID>
<Function>SimpleTypeName(global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod)</Function>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
/// <summary>
/// 方法功能描述
/// </summary>
/// <param name="request">The request.</param>
/// <returns></returns>
public ProductBaseResponse<XXX> GetPromotion(BaseRequest request)
{
if (request == null)
{
return new ProductBaseResponse<XXX> { ResCode = (int)ResultCode.PE_请求参数错误, ResMsg = "请求信息不能为空" };
}
return ClientWrap.Action<ProductBaseResponse<XXX>>(Enum.ServiceGroupEnum.XXX, request, null).Result();
$end$
}]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
保存。
导入自定义代码片段
在代码片段管理器中选中你要导入的文件夹,点击“导入”按钮,选择你刚才编辑的代码片段文件,确认即可。
使用自定义代码片段
在VS代码编辑器里输入bpr,然后敲一下Tab键,则会自动补全你在代码片段里写的代码啦。
节点说明
- <Title>表示此代码片段的标题
- <Shortcut>快捷方式
- <Description>对代码片段的描述
- <SnippetTypes>可以包含多个<SnippetType>,但其取值只有三种:Expansion、SurroundsWith、Refactoring。
Expansion允许代码插入在光标处;
SurroundsWith允许代码围绕在选中代码两边;
Refactoring指定了C#重构过程中所使用的Snippet,在自定义Snippet中不能使用。如果该值不做设置,则Snippet可以放在任何地方。
<Snippet>节点是实现代码管理的地方,其包含四个子节点<Code><Declarations><Imports><References>
1.<Code>
包含<![CDATA[]]>中,放置模版代码,此节点设置Language(C# VB XML),Kind(类型:如方法体,方法声明),Delimiter(分隔符,默认值是$)
2.<Declarations>
包含多个<Literal>和<Object>节点,<Literal>用于指定文本值<Object>用于声明模版对象。笔者自理解为一个函数。以便code调用.
3.<Imports>
引入命名空间,只支持vb . - -#.
4.<References>
添加程序集引用,只支持vb . - -#.
参考:Visual Studio 的代码片段(Code Snippet)功能
扩展:Silverlight Code Snippets for DependencyProperties and other Handy Stuff
函数只适合于C# 总共3个函数
1.GenerateSwitchCases(EnumerationLiteral),根据枚举生成switch代码.
2.ClassName() 返回类名:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>classname</ID>
<Function>ClassName()</Function>
<Default>ClassNamePlaceholder</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<!--[CDATA[
public class $EventHandlerType$
{
//to do ....
}]]-->
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
3.SimpleTypeName(TypeName),在Snippet所在的上下文中推断出TypeName参数的最简单形式:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>EventHandlerType</ID>
<Function>SimpleTypeName(global::System.EventHandler)</Function>
</Literal>
</Declarations>
<Code Language="csharp">
<!--[CDATA[
public class $EventHandlerType$
{
// to do ...
}
}]]-->
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
3、扩展阅读
http://blog.nerdplusart.com/archives/silverlight-code-snippets