我们创建了一个小插件来添加一个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#中,括号已正确放置。
答案 0 :(得分:1)
我已将您的问题转载并在DevExpress支持中心注册。欢迎您跟踪其状态here。修复后,您可以在support @ devexpress.com上的支持团队请求包含修复的构建。目前,作为一种解决方法,您可以替换这行代码:
objThrow.Expression = objNewException
进入这个:
objThrow.Expression = New SnippetExpression(CodeRush.Language.GenerateExpressionCode(objNewException) + vbCrLf)
这将在Visual Basic中正确生成try / catch块。