1

我们创建了一个小插件来添加一个 xml 注释块并为函数创建一个 try-catch。(我们只是将它添加到我们编写的每个函数中)但是在最新的 devexpress 更新中,我遇到了以下代码的问题。

Private Sub cpAddComment_Apply(ByVal sender As System.Object, ByVal ea As DevExpress.CodeRush.Core.ApplyContentEventArgs) Handles cpAddXMLCommentAndTryCatch.Apply
    ' create elementbuilder and add current code to it
    Dim objMethod As New Method
    objMethod = objOldMethod.Clone()
    objElementBuilder.AddStatement(Nothing, objMethod)

    ' add try
    Dim objTry As DevExpress.CodeRush.StructuralParser.Try = objElementBuilder.AddTry(objMethod)
    Dim objCatch As DevExpress.CodeRush.StructuralParser.Catch = objElementBuilder.AddCatch(objMethod, "Exception", "ex")

    ' add exception
    Dim strErrorString As String = """Error in " + objMethod.Location + """, ex"
    Dim objThrow As New DevExpress.CodeRush.StructuralParser.Throw

    Dim objException As New DevExpress.CodeRush.StructuralParser.TypeReferenceExpression("Exception")
    Dim objExceptionString As New DevExpress.CodeRush.StructuralParser.PrimitiveExpression(strErrorString)
    Dim objNewException As New DevExpress.CodeRush.StructuralParser.ObjectCreationExpression(objException)
    objNewException.AddArgument(objExceptionString)
    objThrow.Expression = objNewException
    'objThrow.AddFooter(" ") 'This isnt working either
    objElementBuilder.AddThrow(objCatch, objThrow)


    ' substitute code
    Dim newCode As String = objElementBuilder.GenerateCode()
    ea.TextDocument.Replace(objOldMethod.Range, newCode, "Update Method", True)
end sub

它不会生成正确的 Try-catch 块,而是生成以下不正确的代码:

    Try
    Catch ex As Exception
    Throw New Exception("Error in test", ex)End Try

奇怪的是,以下代码似乎有效(它的代码大致相同,但事件处理程序显示消息框而不是异常)

If not CodeRush.Language.ActiveExtension.DotNetLanguageType = DotNetLanguageType.CSharp Then
    Dim objExceptionString As New DevExpress.CodeRush.StructuralParser.PrimitiveExpression("Messagebox.Show(" + strErrorString + ")" + vbCrLf)
    objElementBuilder.AddStatement(objCatch, objExceptionString)
Else

这个问题在 Vb.Net 中存在,但在 C# 中括号放置正确。

4

1 回答 1

1

我已经复制了您的问题并在 DevExpress 支持中心注册了它。欢迎您在此处跟踪其状态。修复后,您可以通过 support@devexpress.com 向支持团队请求包含修复的构建。现在,作为一种解决方法,您可以替换这行代码:

objThrow.Expression = objNewException

进入这个:

objThrow.Expression = New SnippetExpression(CodeRush.Language.GenerateExpressionCode(objNewException) + vbCrLf)

这将在 Visual Basic 中正确生成 try/catch 块。

于 2011-07-08T12:14:23.600 回答