保存失败后运行resharper静默清理

时间:2012-05-31 20:10:13

标签: resharper code-cleanup visual-studio-macros

我正在尝试使用Resharper的清理代码功能清理我的代码。我创建了一个处理DocumentSaved事件的宏。重要的部分:

Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
                                         Handles DocumentEvents.DocumentSaved
    ...
    DTE.ExecuteCommand("ReSharper_SilentCleanupCode")
    document.Save()
End Sub

每次保存时,都会收到一条异常消息,上面写着Error HRESULT E_FAIL has been returned from a call to a COM component.任何想法?

注意:我看到How can I configure ReSharper's code cleanup on save?并且它在我的情况下不起作用,因为我需要响应save事件。将宏映射到CTRL + S是不够的。

1 个答案:

答案 0 :(得分:0)

我认为这是因为当你打电话给document.Save()时,它会再次出现并再次保存,然后就会失败。试试这个:

Private Sub DocumentEvents_DocumentSaved(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentSaved
    Static currentDocument As EnvDTE.Document

    If Not currentDocument Is Document Then
        currentDocument = Document
        DTE.Windows.Item(Document.Name).Activate()
        DTE.ExecuteCommand("ReSharper_SilentCleanupCode")
        DTE.ActiveDocument.Save()
    End If
End Sub

这对我有用