2
是什么导致vb.net编译器将Option Strict Off添加到为asmx Web引用创建的Reference.vb文件中?有时它在那里,有时它不是,似乎从来没有必要。Option Strict Off在编译器生成的Reference.vb文件中
是什么导致vb.net编译器将Option Strict Off添加到为asmx Web引用创建的Reference.vb文件中?有时它在那里,有时它不是,似乎从来没有必要。Option Strict Off在编译器生成的Reference.vb文件中
由于该文件是有这种类似的标题(版本号可能会有所不同):
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:4.0.30319.42000
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
并率先声明:
Option Strict Off
Option Explicit On
我要说的是,工具生成文件使用VBCodeProvider Class来使用CodeCompileUnit Class生成代码,并且该工具的作者未覆盖UserData
项AllowLateBound
和RequireVariableDeclaration
的默认值。
使用反射器获得的Microsoft.VisualBasic.VBCodeGenerator.GenerateCompileUnitStart
方法的代码如下。
Protected Overrides Sub GenerateCompileUnitStart(ByVal e As CodeCompileUnit)
MyBase.GenerateCompileUnitStart(e)
MyBase.Output.WriteLine("'------------------------------------------------------------------------------")
MyBase.Output.Write("' <")
MyBase.Output.WriteLine(SR.GetString("AutoGen_Comment_Line1"))
MyBase.Output.Write("' ")
MyBase.Output.WriteLine(SR.GetString("AutoGen_Comment_Line2"))
MyBase.Output.Write("' ")
MyBase.Output.Write(SR.GetString("AutoGen_Comment_Line3"))
MyBase.Output.WriteLine(Environment.Version.ToString)
MyBase.Output.WriteLine("'")
MyBase.Output.Write("' ")
MyBase.Output.WriteLine(SR.GetString("AutoGen_Comment_Line4"))
MyBase.Output.Write("' ")
MyBase.Output.WriteLine(SR.GetString("AutoGen_Comment_Line5"))
MyBase.Output.Write("' </")
MyBase.Output.WriteLine(SR.GetString("AutoGen_Comment_Line1"))
MyBase.Output.WriteLine("'------------------------------------------------------------------------------")
MyBase.Output.WriteLine("")
If Me.AllowLateBound(e) Then
MyBase.Output.WriteLine("Option Strict Off")
Else
MyBase.Output.WriteLine("Option Strict On")
End If
If Not Me.RequireVariableDeclaration(e) Then
MyBase.Output.WriteLine("Option Explicit Off")
Else
MyBase.Output.WriteLine("Option Explicit On")
End If
MyBase.Output.WriteLine()
End Sub
...
Protected Function AllowLateBound(ByVal e As CodeCompileUnit) As Boolean
Dim obj2 As Object = e.UserData.Item("AllowLateBound")
If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Boolean) Then
Return CBool(obj2)
End If
Return True
End Function
Protected Function RequireVariableDeclaration(ByVal e As CodeCompileUnit) As Boolean
Dim obj2 As Object = e.UserData.Item("RequireVariableDeclaration")
If ((Not obj2 Is Nothing) AndAlso TypeOf obj2 Is Boolean) Then
Return CBool(obj2)
End If
Return True
End Function
有时Reference.vb文件有它设置为开,有时为关。这是怎么解释的? – jmoreno
@jmoreno,你是对的,我没有解决这个问题。我对此没有多少经验,但在尝试重新创建观察时,发现如果添加服务引用,则Option Strict将处于On状态,并且如果将它添加为Web Reference Option Strict,则会关闭。服务引用使用“WCF代理生成器”,Web引用使用“MSDiscoCodeGenerator”工具生成“Reference.vb”文件。 – TnTinMn