在编译器生成的Reference.vb文件中选项Strict Off

时间:2017-02-24 13:19:04

标签: vb.net asmx

是什么原因导致vb.net编译器将Option Strict Off添加到为asmx Web引用创建的Reference.vb文件中?有时它会在那里,有时它不会,而且似乎从来没有必要。

1 个答案:

答案 0 :(得分:2)

由于该文件的标题类似(您的版本号可能不同),所以:

'------------------------------------------------------------------------------
' <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的默认值商品AllowLateBoundRequireVariableDeclaration

使用Reflector获得的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