Search code examples
vb.netasmx

Option Strict Off in compiler generated Reference.vb file


What causes the vb.net compiler to add Option Strict Off to the Reference.vb file created for asmx web references? Sometimes it's there and sometimes it's not, and it never seems to be necessary.


Solution

  • As the file is has a header similar (your version number may differ) to this:

    '------------------------------------------------------------------------------
    ' <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>
    '------------------------------------------------------------------------------
    

    and the first to statements are:

    Option Strict Off
    Option Explicit On
    

    I would say that the tool that generates the file is using the VBCodeProvider Class to produce the code using the CodeCompileUnit Class and that author of the tool did not override the defaults for the UserData items AllowLateBound and RequireVariableDeclaration.

    The code for the Microsoft.VisualBasic.VBCodeGenerator.GenerateCompileUnitStart method obtained using Reflector is as follows.

    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