我正在攻击一些旧的VB代码,如果发现异常,我想要一个函数提前返回,但是如果它是一个System.UnauthorizedAccessException函数应该继续.所以我没有得到XY'ed,我知道这是一个奇怪的要求,但我正在用C#重写代码,我只需要查看结果.我知道可能有更好的方法.这是原始代码:
Try
doSomeStuffWithFiles(files)
Catch ex As Exception
MsgBox("Far Field: error in reading / writing to far field file." & Chr(13) & ex.Message)
Exit Sub
End Try
Run Code Online (Sandbox Code Playgroud)
所以我添加了几行:
Catch ex As Exception
MsgBox("Far Field: error in reading / writing to far field file." & Chr(13) & ex.Message)
If TypeOf ex IsNot System.UnauthorizedAccessException Then
Exit Sub
End If
End Try
Run Code Online (Sandbox Code Playgroud)
现在,我不是VB的专家,但据我所知,这是完全有效的VB.它还与MSDNTypeOf上的示例代码完全匹配.但是,此代码无法编译.我收到此错误:
Error 21 'Is' expected. C:\FilePath 3114 26 Project
Error 22 'UnauthorizedAccessException' is a type in 'System' and cannot be used as an expression. C:\FilePath 3114 32 Project
Run Code Online (Sandbox Code Playgroud)
如果我将该行更改为
Catch ex As Exception
MsgBox("Far Field: error in reading / writing to far field file." & Chr(13) & ex.Message)
If TypeOf ex Is System.UnauthorizedAccessException Then
Exit Sub
End If
End Try
Run Code Online (Sandbox Code Playgroud)
然后一切都编译好了.(没有逻辑倒退)
我正在使用visual studio 2013,并以.net framework 2.0为目标.
那么什么IsNot是无效的原因?
它可以在Visual Studio 2015中使用它,但是如果你看一下VS2013版本的文档,你会看到只TypeOf ... Is列出,所以你需要使用它Not TypeOf ... Is.
目标.NET Framework版本没有什么区别.如果您使用的是VS2015,TypeOf ... IsNot则会编译.