Coderush 插件未生成正确的 try catch end try 块

发布于 2024-11-19 00:32:25 字数 2138 浏览 5 评论 0原文

我们创建了一个小插件来添加 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# 中括号放置正确。

We've created a little plugin to add a block of xml comment and create a try-catch to a function. (we simply add this to each and every function we write)
But with the latest devexpress update I'm having a problem with the following code.

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

Instead of generating a correct Try-catch block it generates the following incorrect code:

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

Strangely enough the following code seems to work(its about the same code but then for event handlers to show a messagebox instead of an exception)

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

This problem exists in Vb.Net but in C# the brackets are correctly placed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2024-11-26 00:32:25

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

objThrow.Expression = objNewException

为:

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

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

I have reproduced your issue and registered it in DevExpress Support Center. You are welcome to track its status here. Once it is fixed, you can request a build containing the fix from the Support Team at support @ devexpress.com. For now, as a work-around, you can replace this line of code:

objThrow.Expression = objNewException

into this:

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

This will generate the try/catch block correctly in Visual Basic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文