【问题标题】:Is there a method to swap the left and right hand sides of a set of expressions in Visual Studio?有没有一种方法可以在 Visual Studio 中交换一组表达式的左侧和右侧?
【发布时间】:2010-10-13 05:20:12
【问题描述】:

我有一组大小合适的数据需要存储在活动记录中。为了预先填充页面上的表单字段,我已经编写了以下代码:

Device device = new Device(DeviceID); // device is simply the active record

txtDeviceName.Text = device.Name;
txtNotes.Text = device.Notes;
txtHostName.Text = device.Hostname;
txtAssetTag.Text = device.AssetTag;
txtSerialNumber.Text = device.SerialNumber;
// snip... the list goes on!

是否有某种方法(内置功能、宏等)可以用来交换表达式的每一侧,以便将数据保存到活动记录中,而不是按顺序读取执行数据库插入?例如,高亮上面的代码并运行宏后,会变成:

device.DeviceName = txtDeviceName.Text;
device.Notes = txtNotes.Text;
device.Hostname = txtHostName.Text;
device.AssetTag = txtAssetTag.Text;
device.SerialNumber = txtSerialNumber.Text;
// snip again...

由于此活动记录封装的数据库中的列数相当多,因此似乎可以通过简单的自动化过程来避免大部分输入。

显然这不会 100% 有效,因为有时必须进行类型转换(例如,intstring),但在大多数情况下,我认为这会节省很多时间。

【问题讨论】:

    标签: c# .net vb.net visual-studio macros


    【解决方案1】:

    这是一个可以做到这一点的宏:

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports System.Diagnostics
    Imports System.Text.RegularExpressions
    
    Public Module Helpers
        Sub SwapAssignment()
            If (Not IsNothing(DTE.ActiveDocument)) Then
                Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)
                selection.Text = Regex.Replace(selection.Text, "([^\s]*)\s*=\s*([^\s]*);", "$2 = $1;")
            End If
        End Sub
    End Module
    

    基本上,它采用选定的文本(一行或您选择的多行)并使用正则表达式来交换值。不漂亮,但宏永远不会。

    【讨论】:

    • 整洁!有没有办法在整个文本块上执行它而不是仅仅一行?
    • 奇怪,当我一次选择多个时,它只适用于第一行。你用的是VS2008吗?
    • 啊——我的错!我现在已经修好了。
    • 请注意最后一个只需要一次演员的修复。
    【解决方案2】:

    无法让它与正则表达式一起使用(必须是 visualstudio 2010 的东西) 我已经用经典的拆分方式完成了它。它还包括一个检查以跳过 VB 和 c# 注释行。 将它发布给可能需要它的人..

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports System.Diagnostics
    Imports System.Text.RegularExpressions
    
    Public Module Helpers
        Sub SwapAssignment()
            If (Not IsNothing(DTE.ActiveDocument)) Then
                Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)
                Dim lineArray() = selection.Text.Split(vbCrLf)
                Dim output = ""
                For Each line As String In lineArray
                    line = line.Trim()
    
                    If line.Length >= 1 Then
                        If line.Substring(0, 1).Equals("'") OrElse line.Substring(0, 1).Equals("/") Then
                            output &= line & vbCrLf
                            Continue For
                        End If
                    End If
    
                    If line.Contains(" = ") Then
                        Dim splittedline = line.Split("=")
                        output &= splittedline(1) + " = " + splittedline(0) & vbCrLf
                    Else
                        output &= line & vbCrLf
                    End If
                Next
                Dim check = output
                selection.Text = output
            End If
        End Sub
    End Module
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多