17

I was wondering if there is any way to shortcut the process of object initialization with VS 2010 and Resharper (6). When presented with the yellow tool tip below I'd like to have it create an assignment for each field.

For instance, it would generate:

Contact = ; ContactId = ; CreateDate = ; etc, etc

(one for each property).

https://i.stack.imgur.com/Gbcr1.jpg

Emre Erkan
  • 8,433
  • 3
  • 48
  • 53
Chester
  • 171
  • 1
  • 3

2 Answers2

6

ReSharper code completion works exactly this way. As soon as you've typed in the braces, press Ctrl+Space to get the list of all properties:

var data = new Data {|}

Then, after entering a comma to delimit properties, ReSharper completion will only show properties that you haven't yet initialized. More importantly, when you complete a property name, it inserts the property and the equals sign.

Jura Gorohovsky
  • 9,886
  • 40
  • 46
4

Still not answered satisfactorily in another (duplicate) question: Is there a way, at design time, to initialize an object with all properties in Visual Studio 2010?

Closest answer would be to create a custom Code Snippet or Resharper Live Template, but I only see a macro that triggers "smart completion", which is the CTRL+SPACE suggestion seen elsewhere. I'm offering this incomplete answer in case it's easier to remember an alias rather than a shortcut key.

I'd still like it to perform smart completion on that popup list, which is really what the question is asking for. You might also be able to create a custom macro that will accomplish this.

(I'll keep looking and update this answer if I find anything.)

Community
  • 1
  • 1
drzaus
  • 24,171
  • 16
  • 142
  • 201
  • There is some partially helpful info here (auto generating a constructor with all the properties set from arguments): http://stackoverflow.com/questions/2976363/how-do-i-generate-a-constructor-from-class-fields-using-visual-studio-and-or-re – Curtis Yallop Jan 23 '15 at 22:07
  • It seems like a totally useful killer-feature for resharper that they've apparently not thought of. There are multiple stackoverflow questions asking for this and I regularly wish for it. – Curtis Yallop Jan 23 '15 at 22:13
  • I often use sublime's search+multiple-selection to do it if there are many properties. (Specifically: copy the original class into a new blank editor tab, search (ctrl-f) "public", alt-enter to select all occurrences, ctrl-right/end to reach the property names, ctrl-c to copy the names of all the properties, ctrl-a, paste, ctrl-a, ctrl-l (L) to edit all the lines at once, append " = ,"). – Curtis Yallop Jan 23 '15 at 22:18
  • 1
    did you ever find more on this? – user230910 Dec 10 '15 at 05:22