Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2008 class diagram creates empty properties, not autoproperties

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?

like image 850
Ryan Lundy Avatar asked Nov 14 '22 13:11

Ryan Lundy


1 Answers

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!

like image 69
Kevin Berridge Avatar answered Dec 27 '22 21:12

Kevin Berridge