zoukankan      html  css  js  c++  java
  • Visual Studio 如何使用代码片段Code Snippet提高编程速度!!!

     

    使用Code Snippet简化Coding  在开发的项目的时候,你是否经常遇到需要重复编写一些类似的代码,比如是否经常会使用 for、foreach ? 在编写这两个循环语句的时候,你是一个字符一个字符敲还是使用 Visual Studio 提供的Code Snippet 工具自动帮你生成呢?
    神奇之处
      你只需要在代码编辑器中输入for,就会看到 Visual Studio 的自动提示框中出现了如下红框框起来的部分,这个时候只需要连按两下 tab 键,便会自动补全 for 循环语句(如图2所示),并且默认选中索引,以便你进行修改。


    图 1  自动提示框


    图 2  自动补全循环语句
      是不是很神奇? 与之类似的还有switch、cw (Console.WriteLine)、ctor(构造器)、prop(属性)等,灵活运用这些就可以让你的 Coding 工作事半功倍,更多默认 CodeSnippet 请查看参考资源[1]。
      但是,每个项目都有每个项目的特殊性,默认的这些 Code Snippet 如果无法满足您的需求,这个时候就需要自动动手来扩展了。
    Code Snippet 的神秘面纱【C#】
      在动手开始写自己的 Code Snippet 之前,得先搞清楚这个玩意儿是怎么做到的。
      它其实只是一个 xml,只不过包含了一些只有 Visual Studio 才认识的元素,这些元素就定义了如何去替我们补全代码(,Code Snippet 针对不同的语言如VB、C#、CSS使用不同的元素,本文全部按照 C# 语言进行介绍)。

    attribute.Snippet 文件源码  :C:Program Files (x86)Microsoft Visual Studio 14.0VC#Snippets2052Visual C#   

    <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>attribute</Title>
                <Shortcut>Attribute</Shortcut>
                <Description>使用建议模式的特性的代码片段</Description>
                <Author>Microsoft Corporation</Author>
                <SnippetTypes>
                    <SnippetType>Expansion</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
                <Declarations>
                    <Literal>
                        <ID>name</ID>
                        <ToolTip>特性的名称</ToolTip>
                        <Default>My</Default>
                    </Literal>
                    <Literal>
                        <ID>target</ID>
                        <Default>All</Default>
                    </Literal>
                    <Literal>
                        <ID>inherited</ID>
                        <Default>false</Default>
                    </Literal>
                    <Literal>
                        <ID>allowmultiple</ID>
                        <Default>true</Default>
                    </Literal>
                    <Literal Editable="false">
                        <ID>SystemAttribute</ID>
                        <Function>SimpleTypeName(global::System.Attribute)</Function>
                    </Literal>
                    <Literal Editable="false">
                        <ID>SystemAttributeUsage</ID>
                        <Function>SimpleTypeName(global::System.AttributeUsage)</Function>
                    </Literal>
                    <Literal Editable="false">
                        <ID>SystemAttributeTargets</ID>
                        <Function>SimpleTypeName(global::System.AttributeTargets)</Function>
                    </Literal>
                    <Literal Editable="false">
                        <ID>Exception</ID>
                        <Function>SimpleTypeName(global::System.NotImplementedException)</Function>
                    </Literal>
                </Declarations>
                <Code Language="csharp"><![CDATA[[$SystemAttributeUsage$($SystemAttributeTargets$.$target$, Inherited = $inherited$, AllowMultiple = $allowmultiple$)]
    sealed class $name$Attribute : $SystemAttribute$
    {
        // See the attribute guidelines at
        //  http://go.microsoft.com/fwlink/?LinkId=85236
        readonly string positionalString;
        
       // This is a positional argument
       public $name$Attribute (string positionalString)
       {
            this.positionalString = positionalString;
            
           // TODO: Implement code here
           $end$throw new $Exception$();       
       }
       
        public string PositionalString
        {
            get { return positionalString; }
        }
       
       // This is a named argument
       public int NamedInt { get; set; }
    }]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>

    待续。。。

  • 相关阅读:
    【MVC 1】MVC+EF实体框架—原理解析
    数据结构导论(一)
    【JavaScript 14—学习总结】:从小事做起
    【JavaScript 13—应用总结】:锁屏遮罩
    【JavaScript 12—应用总结】:弹出登录框
    【JavaScript 11—应用总结】:下拉菜单
    【JavaScript 10—应用总结】:连缀
    【EF 1】EF实体框架 原理+实例
    【多 线 程】
    【命 令 行】
  • 原文地址:https://www.cnblogs.com/jicheng/p/5950741.html
Copyright © 2011-2022 走看看