English 中文(简体)
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?

问题回答

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!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签