【问题标题】:Code generator tool to generate a property and backing field用于生成属性和支持字段的代码生成器工具
【发布时间】:2010-10-10 09:04:54
【问题描述】:

我正在使用 VS2008 和 C#,我正在寻找一个(免费)代码生成器工具来生成带有 getter 和 setter 的属性,以及要使用的支持私有字段。 VS 中的模板事物并没有使该领域与之相匹配。只是在寻找更好的东西。

我曾经看到一个网站,您可以在其中构建此代码,然后将其从网页中粘贴到您的代码中。

【问题讨论】:

标签: c#


【解决方案1】:

您可以创建自定义 sn-ps 来做几乎任何您想做的事情。这是我在 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\&lt;YOU&gt;\My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets"

【讨论】:

  • 它工作正常,但它都是内联的:bool _Visible; public bool Visible { get { return this._Visible; } 设置 { this._Visible = value; } } 如何添加回车来扩展它?
  • 由于换行符在 CDATA 部分中,它们应该被保留。很奇怪 - 我在我的机器上看到了换行符 :)
  • 啊……我明白了。它在从网页剪切和粘贴到我创建的新 XML 文件时出错了。我修复了它,现在它可以工作了!真棒帮助。谢谢。
【解决方案2】:

在发布之前,我在 StackOverlfow 上环顾四周,试图找到答案,因为我确信之前已经解决了这个问题。我讨厌发帖,但我确实是先看的,我保证。

我一直在寻找更多,我在这里找到了另一个有用的线程:

How to generate getters and setters in Visual Studio?

【讨论】:

    【解决方案3】:

    使用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) %>
    <% } %>
    

    【讨论】:

      【解决方案4】:

      我相信您已经知道,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; }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-10-28
        • 2020-02-17
        • 2012-07-06
        • 2021-03-03
        • 1970-01-01
        • 1970-01-01
        • 2014-09-08
        • 1970-01-01
        • 2013-02-01
        相关资源
        最近更新 更多