Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

c# - How can I reverse code around an equal sign in Visual Studio?

After writing code to populate textboxes from an object, such as:

txtFirstName.Text = customer.FirstName;
txtLastName.Text = customer.LastName;
txtAddress.Text = customer.Address;
txtCity.Text = customer.City;

is there way in Visual Studio (or even something like Resharper) to copy and paste this code into a save function and reverse the code around the equal sign, so that it will look like:

customer.FirstName = txtFirstName.Text;
customer.LastName = txtLastName.Text;
customer.Address = txtAddress.Text;
customer.City = txtCity.Text;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Before VS2012:

  • Copy and paste the original block of code
  • Select it again in the place you want to switch
  • Press Ctrl-H to get the "Replace" box up
  • Under "Find what" put: {[a-zA-Z.]*} = {[a-zA-Z.]*};
  • Under "Replace with" put: 2 = 1;
  • Look in: "Selection"
  • Use: "Regular expressions"
  • Hit Replace All

With VS2012 (and presumably later) which uses .NET regular expressions:

  • Copy and paste the original block of code
  • Select it again in the place you want to switch
  • Press Ctrl-H to get the "Replace" box up
  • Under "Find what" put: ([a-zA-Z.]*) = ([a-zA-Z.]*);
  • Under "Replace with" put: ${2} = ${1};
  • Make sure that the .* (regular expressions) icon is selected (the third one along under the replacement textbox)
  • Hit Replace All

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...