摘要:
为解决代码注释中添加修改日期时间的需求,发现Visual Commander插件可为VS2026扩展功能。通过编写简单C#代码实现自动插入当前时间功能,关键是将Selection转换为TextSelection类型。该插件官网提供51个示例(部分VB代码),可借助AI转换为C#代码满足多种开发需求。
在编写代码的时候,希望在方法的注释中添加一个修改日期和时间。
使用Code Snippet 发现不行。
找插件,也没找到。
不过找到了这个插件,Visual Commander可以为VS2026简单地扩展一下功能。
官网:https://vlasovstudio.com/visual-commander/commands.html
一段简单的代码就可以完成需要的功能
csharp
using System;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
public class InsertDateTime : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
// 确保有活动文档
if (DTE.ActiveDocument == null) return;
// 获取当前日期时间
var date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
// 关键:将 Selection 转换为 TextSelection 类型
var selection = DTE.ActiveDocument.Selection as TextSelection;
if (selection != null)
{
selection.Text = date;
}
}
}
官方示例写了51个,覆盖了大部分需求,但有部分是VB代码。这些代码使用AI可以很容易地转成C#代码。