【问题标题】:Coderush plugin not generating correct try catch end try blockCoderush 插件未生成正确的尝试捕获结束尝试块
【发布时间】:2011-07-07 09:04:05
【问题描述】:

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

【问题讨论】:

    标签: vb.net try-catch coderush dxcore


    【解决方案1】:

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

    objThrow.Expression = objNewException
    

    进入这个:

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

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

    【讨论】:

    • 因为我们有一个通用插件生成 C# 和 Vb.Net 的代码,所以我添加了一个条件。 CodeRush.Language.ActiveExtension.DotNetLanguageType DotNetLanguageType.CSharp
    • 您也可以使用 CodeRush.Language.IsCSharp 属性。
    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多