TypeLoadException only in VS2013 and only for VB.NET

131 views Asked by At

I've tried everything in my knowledge to fix this but as I can't here I am asking for help.

Basically I have an issue with some VB.NET code that throws a TypeLoadException. This is code that was written ages ago and has always worked without a problem.

  • If the code is built in VS2013 it fails.
  • If the code is built in VS2017 it works.
  • If the project is built in VS2017 and opened in VS2013 (without rebuilding) is works. After a rebuild, problem comes back.

I converted the code to C# and it works even if built in VS2013.

The only thing that I have done recently is to clean up temporary files in my system (Win7 64) so I am thinking maybe something happened due to this but my knowledge of Visual Studio isn't sufficient to understand what could be happening.

Before I try to repair VS2013 wanted to know if this happened to someone else and if a solution was found.

To add to the confusion the problem exists only with generic classes!

I have created sample code to run in a console application to replicate the problem and here it is:

Imports System.Runtime.CompilerServices

Module Module1

    Sub Main()

        Dim ng As New NonGenericClass
        ng.WriteToLog() ' Works

        Dim g = New GenericClass(Of IO.FileInfo)
        g.WriteToLog() ' Fails with TypeLoadException

    End Sub

End Module

Public Class GenericClass(Of T)
    Implements IHasLogger

    Public Sub WriteToLog()
        Log.Info("some message")
    End Sub


End Class

Public Class NonGenericClass
    Implements IHasLogger

    Public Sub WriteToLog()
        Log.Info("some message")
    End Sub

End Class

Public Interface ILogger

    Sub Info(message As String)

End Interface

Public Class Logger
    Implements ILogger

    Public Sub Info(message As String) Implements ILogger.Info
        Debug.Print(message)
    End Sub
End Class

Public Interface IHasLogger



End Interface

Public Module LoggerMixin

    <Extension>
    Public Function Log(Of T As IHasLogger)(this As T) As ILogger
        Return New Logger
    End Function


End Module

I assume the issue is with the visual basic compiler at this point as c# has no problems.

Using VS2017 is not an option because the project uses controls from DevExpress and I don't have a license for a version compatible with VS2017.

Thanks for your help.

-- UPDATE -- (Exception information)

System.TypeLoadException: Could not load type 'ConsoleApplication1.GenericClass1' from assembly 'ConsoleApplication13, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. at ConsoleApplication1.GenericClass1.WriteToLog() at ConsoleApplication1.Module1.Main() in E:\Projects\Temp\ConsoleApplication13\ConsoleApplication13\Module1.vb:line 13

-- FIX --

As suggested by TnTinMn using Me.Log (as opposed to just Log) allows to circumvent the problem. For now I will go with that. And now that I come to think of it in C# you would need to call this.Log which is equivalent to Me.Log in VB.NET so my comments about it working in C# don't really apply.

Thanks to all for the help. If anyone knows or finds out why this happens I would still like to know :-)

0

There are 0 answers