1
xmlコメントのブロックを追加して関数にtry-catchを作成するための少しのプラグインを作成しました。 (私たちは、これを各関数に追加します) 最新のdevexpressアップデートでは、次のコードに問題があります。Coderushプラグインが正しいtryキャッチを生成していませんend tryブロック
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#ではこの括弧が正しく配置されています。
C#とVb.Net用のコードを生成する一般的なプラグがあるので、条件を追加しました。 CodeRush.Language.ActiveExtension.DotNetLanguageType <> DotNetLanguageType.CSharp – CodingBarfield
CodeRush.Language.IsCSharpプロパティを使用することもできます。 –