【发布时间】:2016-09-24 15:16:50
【问题描述】:
我的代码:
//App, Core.cs
using System;
using System.IO;
using System.Reflection;
namespace Game
{
public static void Main(string[] args)
{
Assembly a = Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mods\\ExampleMod.dll"));
var x1 = a.GetType("PTF_Mod.Mod_Main");
var x2 = x1.GetMethod("OnStart");
var x3 = x2.Invoke(null, new object[] { });
while(true);
}
}
//App, ModCrew.cs
using System;
using System.Reflection;
namespace Engine
{
public static class ModCrew
{
public class Mod
{
public void ItWorks()
{
Console.WriteLine("It works!");
}
}
}
}
//DLL, Mod_Main.cs
using System;
using System.Reflection;
namespace PTF_Mod
{
public static class Mod_Main
{
public static void OnStart()
{
var exe = Assembly.GetCallingAssembly();
Console.WriteLine(exe.Location); //Location is valid
var x = exe.GetType("Engine.ModCrew.Mod", true); //But here I get exception
var y = Activator.CreateInstance(x);
x.GetMethod("ItWorks", BindingFlags.Instance).Invoke(null, object[] { });
}
}
}
例外: mscorlib.dll 中出现“System.TypeLoadException”类型的异常,但未在用户代码中处理
附加信息:Nie można załadować typu 'Engine.ModCrew.Mod' z zestawu 'Game, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'。
【问题讨论】:
-
"Engine.ModCrew+Mod" -
然后 TypeLoadException on "var x3 = x2.Invoke(null, new object[] { });"在 Core.cs 中
-
"在 mscorlib.dll 中发生了 'System.TypeLoadException' 类型的异常,但未在用户代码中处理"
-
哦,等一下,测试你的答案后,我忘记编译dll了
-
@TheChilliPL
OnStart不接受任何参数,改为使用x2.Invoke(null, null);
标签: c#