【问题标题】:Help needed with Visual Studio MacroVisual Studio 宏需要帮助
【发布时间】:2010-07-05 18:32:46
【问题描述】:

我有一个宏可以将版权标头添加到我的 VB 文件中,但不幸的是它的行为不符合预期。

这是宏

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module CopyrightCode
    Sub AddCopyrightHeader()

        Dim doc As Document
        Dim docName As String
        Dim companyName As String = "My Company"
        Dim authorName As String = "rockinthesixstring"
        Dim authorEmail As String = "rockinthesixstring@example.com"
        Dim copyrightText As String = "All code is Copyright © " & vbCrLf & _
        "'      -   My Exceptional Company (http://example.com)" & vbCrLf & _
        "'     All Rights Reserved"

        ' Get the name of this object from the file name
        doc = DTE.ActiveDocument

        ' Get the name of the current document
        docName = doc.Name

        ' Set selection to top of document
        DTE.ActiveDocument.Selection.StartOfDocument()
        DTE.ActiveDocument.Selection.NewLine()

        Dim sb As New StringBuilder
        sb.Append("' --------------------------------")
        sb.Append(vbCrLf)
        sb.Append("' <copyright file=""" & docName & """ company=""" & companyName & """>")
        sb.Append(vbCrLf)
        sb.Append(copyrightText)
        sb.Append(vbCrLf)
        sb.Append("' </copyright>")
        sb.Append(vbCrLf)
        sb.Append("' <author>" & authorName & "</author>")
        sb.Append(vbCrLf)
        sb.Append("' <email>" & authorEmail & "</email>")
        sb.Append(vbCrLf)
        sb.Append("' <date>" & FormatDateTime(Date.Now, vbLongDate) & "</date>")
        sb.Append(vbCrLf)
        sb.Append("' ---------------------------------")

        ' Write first line
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.Text = sb.ToString

    End Sub
End Module

但问题是它在插入的末尾添加了四个引号,我必须手动删除。这些引号是从哪里来的?

' --------------------------------  
' <copyright file="MyFile.vb" company="My Company">  
'     All code is Copyright ©     
'      -   My Exceptional Company (http://example.com) 
'     All Rights Reserved  
' </copyright>  
' <author>rockinthesixstring</author>  
' <email>rockinthesixstring@example.com</email>  
' <date>Monday, July 05, 2010</date>  
' ---------------------------------   
""""  

但是,如果我使用单引号,一切都很好。

    sb.Append("' <copyright file='" & docName & "' company='" & companyName & "'>")

【问题讨论】:

    标签: visual-studio macros


    【解决方案1】:

    没有复制,它们不是来自宏。考虑某种 Visual Studio 插件作为问题的根源。

    【讨论】:

    • 如果我通过文件中的“撤消”后退一步,它们会开始“构建”,因为引号被写在 &lt;copyright file="MyFile.vb" company="My Company"&gt;
    • 所以每次将引用写入文件时,也会写入第二个。
    • 某种 Resharper 风格的工具会在您键入第一个时自动添加第二个“?
    • 啊,就是这样……Resharper。当。好的,我会在这个上坚持使用单引号。