2009-02-27 32 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; } 
}