gpt4 book ai didi

c# - 是否有一种方法可以在 Visual Studio 中交换一组表达式的左右两侧?

转载 作者:太空狗 更新时间:2023-10-29 17:48:58 31 4
gpt4 key购买 nike

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

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),但在大多数情况下我认为这会节省很多时间。

最佳答案

这里有一个宏可以做到这一点:

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

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

关于c# - 是否有一种方法可以在 Visual Studio 中交换一组表达式的左右两侧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/643687/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com