17

I am building .Net Core 2.0 web application that imports a .Net Framework 4.6.1 project class library. The solution builds properly, however, when reaching the below functionality, I receive this error:

System.TypeLoadException: Could not load type 'Microsoft.VisualBasic.Information' from assembly 'Microsoft.VisualBasic, Version=10.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

I have found Microsoft.VisualBasic.dll in: C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualBasic\v4.0_10.0.0.0__b03f5f7f11d50a3a

However, the reference does not remain checked when attempting to add the it via Project -> Add Reference and browsing for it. I can check it, but when I return to the Reference Manager, the DLL is unchecked again.

The references and imported namespaces in the class library are as follows: Image 1

Image 2

Image 3

In case it matters, the functionality uses reflection, and is as follow (imports shown in case):

    Imports System.Data.SqlClient
    Imports System.Reflection
    Imports Microsoft.Win32


    Shared Function GetContentCostFieldMapping(Optional ByVal busGrp As String = Nothing, Optional ByVal program As String = Nothing) As CostFieldMapping
        Dim _conn As SqlConnection = Nothing
        Dim _row As CostFieldMapping = New CostFieldMapping

        Try
            _conn = New SqlConnection(_sqlDB)
            _conn.Open()

            Dim _cmd As SqlCommand
            Dim _dr As SqlDataReader

            _cmd = New SqlCommand("GetContentCostFieldMapping", _conn)
            _cmd.CommandType = CommandType.StoredProcedure
            _cmd.Parameters.Add("@BusGrp", SqlDbType.NVarChar).Value = busGrp
            _cmd.Parameters.Add("@Program", SqlDbType.NVarChar).Value = program

            _dr = _cmd.ExecuteReader

            _row = SharedManager.GenericGet(Of CostFieldMapping)(_dr, False)
            Return _row
        Catch ex As Exception
            Helpers.LogMessage("Error: " + ex.Message + ". Stacktrace: " + ex.StackTrace)
        Finally
            If _conn IsNot Nothing AndAlso Not _conn.State = ConnectionState.Closed Then
                _conn.Close()
            End If
        End Try

        Return _row
    End Function

    Public Shared Function GenericGet(Of T As {Class, New})(dr As SqlDataReader, ByVal listFlag As Boolean)
        Dim results As Object
        If listFlag Then
            results = New List(Of T)()
        End If

        Dim businessEntityType As Type = GetType(T)
        Dim hashtable As New Hashtable()
        Dim properties As PropertyInfo() = businessEntityType.GetProperties()
        For Each info As PropertyInfo In properties
            hashtable(info.Name.ToUpper()) = info
        Next
        While dr.Read()
            Dim newObject As New T()
            For index As Integer = 0 To dr.FieldCount - 1
                Dim info As PropertyInfo = DirectCast(hashtable(dr.GetName(index).ToUpper()), PropertyInfo)
                If (info IsNot Nothing) AndAlso info.CanWrite AndAlso IsDBNull(dr.GetValue(index)) = False Then
                    SetValue(newObject, info, dr.GetValue(index))
                End If
            Next

            If listFlag Then
                results.Add(newObject)
            Else
                results = newObject
            End If
        End While
        dr.Close()
        Return results
    End Function

Not being able to add the DLL is the main issue, however, as I continue to debug, the debugger keeps throwing errors about missing libraries. Are the two projects incompatible? Is there a clean way to import a .NET Framework code library for use in a .NET core application?

user
  • 1,261
  • 2
  • 21
  • 43
  • 5
    .NET Core implements .NET Standard. Is `IsDBNull` part of .NET Standard? That's a method that should **NEVER** have been used in new VB.NET code anyway. There are alternatives, as used by C# developers since the inception of .NET, and they should be used in VB too. – jmcilhinney Mar 09 '18 at 04:35
  • Thanks for your feedback. I was unaware that IsDBNull is bad practice. Removing it allowed the code to continue. Is there a reference for what functionality is disallowed in .NET Standard? – user Mar 09 '18 at 15:51
  • 4
    It's not a case of what's not allowed in .NET Standard but what is allowed. If you want to know what the .NET Standard includes then you can read about that, but one clue is that things that have never been supported in C# will not be part of the standard. That means anything that is VB-specific and not part of the language itself, which means pretty much anything in the `Microsoft.VisualBasic namespace, e.g. `MsgBox`, `InStr` and `IsDBNull`. – jmcilhinney Mar 09 '18 at 23:48
  • 3
    Take a look here: [Difference between ASP.NET Core (.NET Core) and ASP.NET Core (.NET Framework)](https://stackoverflow.com/questions/37684508/difference-between-asp-net-core-net-core-and-asp-net-core-net-framework/37684644#37684644). This may help you to understand what's the difference between a windows-based (.net framework) and multi-platform (.net core) project. – Maciej Los May 12 '19 at 20:14
  • @jmcilhinney it appears there's another IsDBNull function in System.Convert for C#, and it looks like it does the same thing. ( https://learn.microsoft.com/en-us/dotnet/api/system.convert.isdbnull?view=net-5.0 ) – HardCode Jun 23 '21 at 18:34
  • @HardCode, never used that myself. Both the `DataRow` and all data reader classes have their own method for null checks and in other cases I use `If something Is DbNull.Value Then`. – jmcilhinney Jun 24 '21 at 01:13
  • try upgrading your project to net core 5, i dont think 2.0 or 3.1 will work in your case – Alok Mar 18 '22 at 13:49

0 Answers0