views:

207

answers:

1

I'm using a Class Diagram in Visual Studio 2008 to create some classes with properties. I notice that when I create a new property in the Class Diagram, it comes out in code like this:

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

Yuck. I'd much rather it end up as an autoproperty like this:

public DateTime DateBilled { get; set; }

Is there some way I can change or customize that?

+1  A: 

This isn't exactly what you're looking for, but it might get you close to the result you need.

This is a Visual Studio 2008 Macro which will find the class diagram generated get properties and replace them with auto properties.

  1. In VS go to View -> Other Windows -> Macro Explorer
  2. Right click on "MyMacros" and select "New module..."
  3. Give it any name you'd like
  4. Right click on that and select "New macro"
  5. Paste this code in

Here's the code:

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

This is pretty much just a hack, and I'm not sure if it will work with all the output from the class designer, but it has worked in my testing so far and it certainly saves a few keystrokes.

Hope it helps!

Kevin Berridge