【问题标题】:Visual Studio 2008 class diagram creates empty properties, not autopropertiesVisual Studio 2008 类图创建空属性,而不是自动属性
【发布时间】:2009-12-21 22:35:09
【问题描述】:

我在 Visual Studio 2008 中使用类图来创建一些具有属性的类。我注意到当我在类图中创建一个新属性时,它会以如下代码出现:

public DateTime DateBilled
{
    get
    {
        throw new System.NotImplementedException();
    }
    set
    {
    }
}

哎呀。我更希望它最终成为这样的自动属性:

public DateTime DateBilled { get; set; }

有什么方法可以改变或定制吗?

【问题讨论】:

  • (我可以使用 CodeRush 来转换它们,但我必须一次完成一个。我希望它们一开始就以正确的方式创建。)
  • 另外,我遇​​到了这个问题:modeling.codeplex.com 这是一种解决方案。但也不完全。当我在 Class Details 窗口中输入时,我宁愿将 autoproperties 设为默认值;使用它很难设置属性类型。

标签: c# visual-studio visual-studio-2008 code-generation class-diagram


【解决方案1】:

这并不是您正在寻找的,但它可能会让您接近所需的结果。

这是一个 Visual Studio 2008 宏,它将查找类图生成的获取属性并将其替换为自动属性。

  1. 在 VS 中转到查看 -> 其他窗口 -> 宏资源管理器
  2. 右键单击“MyMacros”并选择“新建模块...”
  3. 给它起任何你喜欢的名字
  4. 右键单击它并选择“新建宏”
  5. 将此代码粘贴到

代码如下:

DTE.ExecuteCommand("Edit.Find")
DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
DTE.Find.FindWhat = "<get$"
DTE.Find.MatchCase = False
DTE.Find.MatchWholeWord = False
DTE.Find.Backwards = False
DTE.Find.MatchInHiddenText = True
DTE.Find.Action = vsFindAction.vsFindActionFind
While DTE.Find.Execute() <> vsFindResult.vsFindResultNotFound
    DTE.ActiveDocument.Selection.LineDown(True, 6)
    DTE.ExecuteCommand("Edit.Delete")
    DTE.ActiveDocument.Selection.Text = "get; set;"
End While

这几乎只是一个 hack,我不确定它是否适用于类设计器的所有输出,但到目前为止,它在我的测试中有效,而且它确实节省了一些击键。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多