c#程式碼片段建立(sinppet)

來源:互聯網
上載者:User

標籤:

在c#裡面我們經常用到

1、if 然後按2下Tab,出來下面一段代碼

if (true){         }

2、propfull

private int myVar;public int MyProperty{      get { return myVar; }       set { myVar = value; }}

……

然後這些在多次項目使用中,總有寫不方便,比如mvvm開發winrt應用 有這樣子屬性寫法

private string _gameId;[JsonProperty("GAME_ID")]public string GameId{    get { return _gameId; }    set { this.Set(ref _gameId, value); }}

  

每次都這麼寫這樣代碼,感覺是不是很卵疼?當然你可以自己寫一個代碼產生器去產生這些屬性,這篇文章重點不是講這個,忽略過。。

 

於是乎作者去百度搜尋 if propfull儲存位置在C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\2052\Visual C#(win8.1 64位系統)

隨便找一個檔案來開啟,本人以profull檔案為例子

<?xml version="1.0" encoding="utf-8"?><CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"><CodeSnippet Format="1.0.0"><Header><Title>propfull</Title><Shortcut>propfull</Shortcut><Description>屬性和支援欄位的程式碼片段</Description><Author>Microsoft Corporation</Author><SnippetTypes><SnippetType>Expansion</SnippetType></SnippetTypes></Header><Snippet><Declarations><Literal><ID>type</ID><ToolTip>屬性類型</ToolTip><Default>int</Default></Literal><Literal><ID>property</ID><ToolTip>屬性名稱</ToolTip><Default>MyProperty</Default></Literal><Literal><ID>field</ID><ToolTip>支援此屬性的變數</ToolTip><Default>myVar</Default></Literal></Declarations><Code Language="csharp"><![CDATA[private $type$ $field$;public $type$ $property${get { return $field$;}set { $field$ = value;}}$end$]]></Code></Snippet></CodeSnippet></CodeSnippets>

應該有一個 Snippet Editor 編輯器,這個貌似是vb版本,

各位自行去百度c#版本 

http://snippy.codeplex.com/releases/view/9316

https://github.com/mmanela/SnippetDesigner

 

各個節點介紹:http://www.cnblogs.com/anderslly/archive/2009/02/16/vs2008-code-snippets.html

 

然後我自己修改代碼如下:

檔案名稱:propvm.snippet

<?xml version="1.0" encoding="utf-8"?><CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"><CodeSnippet Format="1.0.0"><Header><Title>propvm</Title><Shortcut>propvm</Shortcut><Description>屬性和支援欄位的程式碼片段</Description><Author>Microsoft Corporation</Author><SnippetTypes><SnippetType>Expansion</SnippetType></SnippetTypes></Header><Snippet><Declarations><Literal><ID>type</ID><ToolTip>屬性類型</ToolTip><Default>int</Default></Literal><Literal><ID>property</ID><ToolTip>屬性名稱</ToolTip><Default>MyProperty</Default></Literal><Literal><ID>field</ID><ToolTip>支援此屬性的變數</ToolTip><Default>myVar</Default></Literal></Declarations><Code Language="csharp">      <![CDATA[private $type$ $field$;public $type$ $property${get { return $field$;}set {this.Set(ref $field$, value);}}$end$]]></Code></Snippet></CodeSnippet></CodeSnippets>

儲存到目錄:C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\2052\Visual C#(win8.1 64位系統) 開頭我給出目錄

(注意要重啟vs,都不會生效。。重啟電腦這個作者沒試過)

本人是在Tools->code sinppets manager add 上面目錄(C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\2052\Visual C#)進去,然後remove原來visual c#目錄

 

 上面修改地方:

1、set {this.Set(ref $field$, value);}
2、還有快速鍵
<Title>propvm</Title><Shortcut>propvm</Shortcut> 


測試效果:
 private int myVar; public int MyProperty {     get { return myVar; }     set { this.Set(ref myVar, value); } }

其他也很類似,各位根據自己項目擼,作者就不在此裝逼



  

  

 

  

c#程式碼片段建立(sinppet)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.