Domanda

I have a code block that is implicitly adding variables to an array, i.e.

context.People.Add( new Person { Name = "Joe", Age = 45 }, new Person { Name = "Alicia", Age = 33 } );

What I need is for the variables to be accessible outside of the code block, i.e.

var personJoe = new Person { Name = "Joe", Age = 45 };
var personAlicia = new Person { Name = "Alicia", Age = 33 };
context.People.Add(personJoe, personAlicia);

This way I can access the person variables when I need to make an assignment to another entity that has a relationship with person.

My question is, what is the easiest way to perform this refactoring? I understand I can copy/paste, but the block I have is approximately 200 lines long, and I have found myself needing this type of re-factoring often. If at all possible, I would like to avoid paid solutions (ReSharper), but if this is the only way to get this process automated, I will seriously consider it.

È stato utile?

Soluzione

Seems to me this is simply an Introduce Variable refactoring. Visual Studio doesn't have this refactoring built in; but may other add-ons do. If you don't want a paid solution, then the refactoring consists of copy/paste then adding a variable and using it.

To do this in Resharper (with the Visual Studio keyboard scheme), simple select where you instantiate the Person (e.g. new Person { Name = "Alicia", Age = 33 }) and press Ctrl+R,V.

Altri suggerimenti

I just wrote this CodeRush plugin. (It took about 20-30 minutes :) ) I think it does what you're looking for.

Introduce Local (All arguments)

If you've got CodeRush installed you can Download it here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top