Option Strict On 不允许后期绑定 [英] Option Strict On disallows late binding
问题描述
有人可以帮我解决这个错误吗?
Can someone help me fix this error?
Option Strict On 不允许后期绑定
Option Strict On disallows late binding
这是导致错误的代码:
Dim SF6StdData As BindingSource = New BindingSource()
' ...
If StrComp(SF6StdData.Current("O2AreaCts").ToString, "") = 0 Then
AreaCts(3) = 0
Else
AreaCts(3) = Convert.ToDouble(SF6StdData.Current("O2AreaCts").ToString)
End If
我需要重写代码,以免出现任何错误.我知道我可以通过在项目属性中将 Option Strict 设置为 Off 来解决这个问题,但我真的不想这样做.还有其他方法吗?
I need to rewrite the code so it will not have any errors. I know I could fix this by setting Option Strict to Off in the project properties, but I really don't want to do that. Is there some other way?
推荐答案
Option Strict
开启时不允许延迟绑定.如果您需要执行后期绑定,唯一的选择是使用反射或关闭 Option Strict
.然而,一个可取之处是您不必为整个项目关闭 Option Strict
.您可以为项目保留它,然后只需在需要执行后期绑定的任何代码文件的顶部添加行 Option Strict Off
.这不是一个很好的解决方案,但总比影响整个项目要好.
Late binding is not allowed when Option Strict
is on. If you need to perform late binding, the only options are either to use reflection or to shut off Option Strict
. The one saving grace, however, is that you don't have to shut off Option Strict
for the whole project. You can leave it on for the project and then just add the line Option Strict Off
at the top of any code files where you need to perform late binding. It's not a great solution, but it's better than affecting the whole project.
此外,由于位于文件顶部的 Option Strict
仅适用于该文件,因此它甚至不必适用于整个类.如果您将类拆分为多个 Partial Class
文件,那么您可以为每个文件设置不同的 Option Strict
.例如,如果您将大部分类放在一个带有 Options Strict On
的文件中,然后只将一个方法放在 Partial Class
中的一个带有 的单独文件中Option Strict Off
,那么只有一种方法会被松散编译.类的其余部分将使用严格的规则进行编译.
Also, since the Option Strict
placed at the top of a file applies just to that file, it doesn't even have to apply to an entire class. If you split your class into multiple Partial Class
files, then you could have Option Strict
set differently for each of those files. For instance, if you put most of your class in a file with Options Strict On
, and then just put one method in a Partial Class
in a separate file with Option Strict Off
, then only that one method would be compiled loosely. The rest of the class would be compiled using the strict rules.
这篇关于Option Strict On 不允许后期绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!