5

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

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

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

public DateTime DateBilled { get; set; }

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

4

1 回答 1

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,我不确定它是否适用于类设计器的所有输出,但到目前为止它在我的测试中有效,它肯定会节省一些击键。

希望能帮助到你!

于 2009-12-23T16:40:24.863 回答