c# - 如何在 Visual Studio 中围绕等号反转代码?

标签 c# visual-studio-2008

编写代码从对象填充文本框后,例如:

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

在 Visual Studio(或什至像 Resharper 之类的东西)中有没有办法将此代码复制并粘贴到保存函数中并反转等号周围的代码,这样它看起来像:

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

最佳答案

VS2012 之前:

  • 复制并粘贴原始代码块
  • 在要切换的地方重新选择
  • 按 Ctrl-H 调出“替换”框
  • 在“查找内容”下输入:{[a-zA-Z\.]*} = {[a-zA-Z\.]*};
  • 在“替换为”下输入:\2 =\1;
  • 查看:“选择”
  • 使用:“正则表达式”
  • 点击全部替换

对于使用 .NET 正则表达式的 VS2012(大概以后):

  • 复制并粘贴原始代码块
  • 在要切换的地方重新选择
  • 按 Ctrl-H 调出“替换”框
  • 在“查找内容”下输入:([a-zA-Z\.]*) = ([a-zA-Z\.]*);
  • 在“替换为”下输入:${2} = ${1};
  • 确保选择了 .*(正则表达式)图标(替换文本框下方的第三个图标)
  • 点击全部替换

关于c# - 如何在 Visual Studio 中围绕等号反转代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/430145/

相关文章:

visual-studio-2008 - Visual Studio 2010 : It is worth for C++ programming?

visual-studio - 在Visual Studio 2008/2010中的代码中为位置添加书签的最佳方法是什么?

c# - 这个锁使用线程安全吗?

c# - 我应该调用 GC.Collect

c# - 是否可以使用 Oxyplot 创建等值面?

visual-studio-2008 - 如何在 Visual Studio 2008 中打开团队资源管理器

c# - WPF Datagrid 绑定(bind)列表问题

c# - Marshal.QueryInterface() - 如何使用返回值?

c# - C#中的音频描述符MFCC

c# - 如何快速组织源代码(c#)中的函数以按字母顺序排序?