2009-02-27 73 views
3

我在VS2008和C#中工作,我正在尋找一個(免費)代碼生成器工具來生成一個屬性與getter和setter,以及後備私人領域去。 VS中的模板並沒有使這個領域與它一致。只是尋找更好的東西。代碼生成器工具生成一個屬性和支持字段

我曾經看到過一個網站,在這裏你可以建立這段代碼,然後將它從網頁中cust-and-paste粘貼到你的代碼中。

+1

也許你應該嘗試的CodeRush Express來生成屬性:HTTP:/ /www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/ – Oliver 2010-04-07 10:54:08

回答

5

您可以創建自定義代碼段來執行任何您想要的操作。這是一個我在VS2005用於創建與支持字段屬性:

<?xml version="1.0" encoding="utf-8" ?> 
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> 
    <CodeSnippet Format="1.0.0"> 
     <Header> 
      <Title>prop</Title> 
        <!-- the shortcut below will show in your intellisense 
         window - set it to whatever you wish --> 
      <Shortcut>_prop</Shortcut> 
      <Description>Code snippet for a property</Description> 
      <Author>Andrew</Author> 
      <SnippetTypes> 
       <SnippetType>Expansion</SnippetType> 
      </SnippetTypes> 
     </Header> 
     <Snippet> 
      <Declarations> 
       <Literal> 
        <ID>type</ID> 
        <Default>String</Default> 
        <ToolTip>property type</ToolTip> 
       </Literal> 
       <Literal> 
        <ID>pname</ID> 
        <Default>_name</Default> 
        <ToolTip>private field name</ToolTip> 
       </Literal> 
       <Literal> 
        <ID>name</ID> 
        <Default>Name</Default> 
        <ToolTip>property name</ToolTip> 
       </Literal> 
      </Declarations> 
      <Code Language="csharp"> 
        <![CDATA[$type$ $pname$; 

      public $type$ $name$ 
      { 
       get { return this.$pname$; } 
       set { this.$pname$ = value; } 
      }$end$]]> 
      </Code> 
     </Snippet> 
    </CodeSnippet> 
</CodeSnippets> 

保存在一個名爲在這個位置whatever.snippet文件:

"C:\Documents and Settings\<YOU>\My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets"

+0

它工作正常,但其全部內聯: bool _Visibl Ë; public bool可見{get {return this._Visible; } set {this._Visible = value; }} 如何添加Carriage Returns將其全部展開? – MattSlay 2009-02-27 16:57:21

+0

因爲換行符在CDATA部分中,所以應該保留它們。很奇怪 - 我在我的機器上看到換行符:) – 2009-02-27 17:17:29

0

我看了看周圍的StackOverlfow在我發佈之前試圖找到答案,因爲我確信這已經在之前解決了。我討厭張貼,但我確實首先看,我保證。

我一直在尋找更多的,一個我在這裏發現這個其他有用的線索:

How to generate getters and setters in Visual Studio?

0

隨着CodeSmith僅僅是一個點擊即可。有時候不如買工具,而不是重新發明輪子

<%-- 
Name: Database Table Properties 
Authors: Paul Welter , Yordan Georgiev 
Description: Create a list of properties from a database table with a region for each prop 
--%> 
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %> 
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %> 
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %> 
<%@ Assembly Name="SchemaExplorer" %> 
<%@ Import Namespace="SchemaExplorer" %> 

<% foreach (ColumnSchema column in this.SourceTable.Columns) { %> 

#region <%= StringUtil.ToPascalCase(column.Name) %> 
private <%= CSharpAlias[column.SystemType.FullName] %> _<%= StringUtil.ToPascalCase(column.Name) %>; 

public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToPascalCase(column.Name) %> 
{ 
    get { return _<%= StringUtil.ToPascalCase(column.Name) %>; } 
    set { _<%= StringUtil.ToPascalCase(column.Name) %> = value; } 
} 
#endregion <%= StringUtil.ToPascalCase(column.Name) %> 
<% } %> 
0

正如我相信你已經知道,當編譯在VS 2008中的C#編譯器會自動生成getter和setter屬性。這甚至適用於.NET 2.0應用程序,因爲它全部在編譯器中完成。

這意味着你可以這樣做:

public int Number { get; set; } 

而不是

private int _number; 
public int Number 
{ 
get { return this._number; } 
set { this._number = value; } 
}