我正在使用商业 C# 应用程序,该应用程序旨在允许使用应用程序在运行时加载的外部插件。基本上,您创建一个类库来继承和扩展基本应用程序的某些类,并将您的 DLL 放在主应用程序查找它的指定位置。大多数为此应用程序编写的插件使用 C#,但我想改用 VB,我认为这应该是可能的。
但是,我在应用程序的错误日志中得到以下异常:
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”是我的 VB 类的名称。 C# 应用程序和 VB 类都引用 .NET 3.5
我是一名业余程序员,所以请放轻松。我认为发生的事情是我的类库引用了主应用找不到的某些类型(例如:My.MyProject、System.Configuration.ApplicationSettingsBase、MyWebServices)。我试图在类库中添加对 Microsoft.VisualBasic 的引用,但 Visual Studio 返回一个错误,指出该组件“已被构建系统自动引用”。我的这个“测试”代码非常简单:
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
任何想法如何进行?我可以从类库中排除有问题的类型,或者以某种方式告诉 C# 应用程序在哪里可以找到它们吗?或者,我想做的事情根本不可能吗?
更新:
下面的答案(在 VS IDE 之外编译)适用于我的示例代码,但实际上并没有做太多事情。然后我添加了一些外部库,其中一个需要 Newtonsoft.Json.dll。我在编译期间引用了那个 DLL,它编译得很好,但是当我运行应用程序时,我在日志中得到了这个: System.TypeLoadException:无法从程序集“Newtonsoft.Json,Version=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”加载类型“Newtonsoft.Json.Converters.StringEnumConverter”。
System.TypeLoadException:无法从程序集“Newtonsoft.Json,Version=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”加载类型“Newtonsoft.Json.Converters.XObjectWrapper”。
System.TypeLoadException:无法从程序集“Newtonsoft.Json,Version=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”加载类型“Newtonsoft.Json.Linq.Extensions”。
这是同一个问题吗?如果我使用 Newtonsoft 编译,为什么会出现这些错误?
最佳答案
试图欺骗 Unity 以使用 VB?呵呵。
我用 Xamarin 做了类似的事情,为此您必须手动编译 DLL 并从您的库中删除 Microsoft.VisualBasic 引用(然后您将对必须替换为仅使用标准框架函数的所有代码感到畏缩)。
这里的起点是我们用来针对 MonoTouch 进行编译的内容。
C:\WINDOWS\Microsoft.NET\Framework\v3.5\Vbc.exe/sdkpath:(Unity 框架的路径)/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:(你的命名空间)/define:"_MyType=\"Empty\""/reference:(所有你需要的库)/target:library "(path to source code)\*.vb"
关于C# 应用程序无法从 VB 类库中找到类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24412765/