2
votes

I'm using a commercial C# application which is designed to allow the use of external plug-ins that the app loads at runtime. Basically, you create a class library that inherits and extends certain classes from the base app, and place your DLL in a specified place where the main app looks for it. Most plug-ins written for this app use C# however I want to use VB instead, and I think this should be possible.

However, I get the following exceptions in the error log of the app:

AssemblyLoader: Exception loading 'Thingamajig': System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.

at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)

at System.Reflection.Assembly.GetTypes () [0x00000] in :0

at AssemblyLoader.LoadAssemblies () [0x00000] in :0

Additional information about this exception:

System.TypeLoadException: Could not load type 'Thingamajig.My.MyProject' from assembly 'Thingamajig, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

System.TypeLoadException: Could not load type 'Thingamajig.My.MyProject+ThreadSafeObjectProvider`1[T]' from assembly 'Thingamajig, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

System.TypeLoadException: Could not load type 'MyWebServices' from assembly 'Thingamajig, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

System.TypeLoadException: Could not load type 'System.Configuration.ApplicationSettingsBase' from assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

File name: 'Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

"Thingamajig" is the name of my VB class. Both the C# app and the VB class reference .NET 3.5

I'm an amateur programmer, so go easy on me please. I think what is happening is my class library is referencing certain types (Ex: My.MyProject, System.Configuration.ApplicationSettingsBase, MyWebServices) that the main app cannot find. I tried to add a reference to Microsoft.VisualBasic in the class library but Visual Studio comes back with an error stating that this component is "already automatically referenced by the build system". My code for this "test" is very simple:

Imports System
Imports UnityEngine

Public Class Thingamajig
    Inherits PartModule
    Private _windowPosition As New Rect()

    Public Overrides Sub OnStart(state As StartState)
        If state <> StartState.Editor Then
            RenderingManager.AddToPostDrawQueue(0, AddressOf ondraw)
        End If
    End Sub

    Private Sub ondraw()
        If Me.vessel.active Then
            _windowPosition = GUILayout.Window(10, _windowPosition, AddressOf onwindow, "Hello World")
        End If
    End Sub

    Private Sub onwindow(windowid As Integer)
        GUILayout.BeginHorizontal(GUILayout.Width(250.0F))
        GUILayout.Label("WOW! Isn't this Amazing?")
        GUILayout.EndHorizontal()
        GUI.DragWindow()

    End Sub

End Class

Any ideas how to proceed? Can I exclude the problematic types from the class library, or somehow tell the C# app where to find them? Or, is what I am trying to do simply not possible?

UPDATE:

The answer below (compiling outside of the VS IDE) worked for my sample code that didn't really do much. Then I added some external libraries, one of which requires Newtonsoft.Json.dll. I referenced that DLL during compilation and it compiles fine but when I run the app I get this in the log: System.TypeLoadException: Could not load type 'Newtonsoft.Json.Converters.StringEnumConverter' from assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.

System.TypeLoadException: Could not load type 'Newtonsoft.Json.Converters.XObjectWrapper' from assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.

System.TypeLoadException: Could not load type 'Newtonsoft.Json.Linq.Extensions' from assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.

Is this the same issue? If i compile with Newtonsoft, why am I getting these errors?

1
Show us the code that loads the classes. - Robert Harvey
Try to set "Project Properties/Settings/Access Modifier" to "Friend". - dummy
@RobertHarvey the C# is a commercial app, no source code is available. It is designed to be extensible, but I am limited to modifying the VB class library only. - Timbo
Hi. Xamarin has an implementation of Newtonsoft for Monotouch and Monodroid, it will work with Unity: components.xamarin.com/view/json.net - Gusman

1 Answers

1
votes

Triying to cheat Unity to use VB? hehehe.

I did something similar with Xamarin, to do it you must compile manually the DLL and remove the Microsoft.VisualBasic reference from your library (and then you will cringe with all the code you must replace to use only standard framework functions).

As a start point here is what we used to compile against MonoTouch.

C:\WINDOWS\Microsoft.NET\Framework\v3.5\Vbc.exe /sdkpath:(path to Unity framework) /noconfig /verbose /novbruntimeref /imports:System,System.Reflection,System.Collections,System.Collections.Generic /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /optionstrict+ /rootnamespace:(your namespace) /define:"_MyType=\"Empty\"" /reference:(all your needed libraries) /target:library "(path to source code)\*.vb"