1
我想通过输入一个字符串来执行命令。我有以下类:System.TypeLoadException未处理:无法加载类型,因为该方法没有实现(没有RVA)
abstract class ECommand {
public static bool TryExecute(string raw, out object result) {
string name = raw; //function trigger
if (!Config.CommandRegister.ContainsKey(name)) {
result = null;
return false;
}
Type datType = Config.CommandRegister[name];
var instance = Activator.CreateInstance(datType);
MethodInfo method = datType.GetMethod("Execute");
result = method.Invoke(instance, null);
return true;
}
public extern object Execute();
}
字符串和等效类型登记在词典是这样的:
public static Dictionary<string, Type> CommandRegister =
new Dictionary<string, Type> { {"test", typeof(TestECommand)} };
我尝试测试它在这个类:
class TestECommand : ECommand {
public new object Execute() {
Console.WriteLine("test");
return "k";
}
}
致电时它
ECommand.TryExecute(source, out res);
我收到以下例外情况:
System.TypeLoadException was unhandled
HResult=-2146233054
Message=Could not load type 'Test.ECommand' from assembly 'Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'Execute' has no implementation (no RVA).
Source=Test
TypeName=Test.ECommand
我在做什么错?
我猜你含TestECommand组件是旧的,对ECommand编译时ECommand有用于执行()的实现。我也猜测你已经将ECommand.Execute更改为抽象,移除了实现,而无需重新编译托管TestECommand的程序集。如果我的猜测是正确的,重新编译(和清理旧资源)将解决问题。 (还要确保你没有旧文件引用,这些引用在重新编译时没有更新。) – sisve
我没有改变代码。而且一切都在一个项目中,所以我没有看到某些东西可能过时的地步?在我的进一步研究中,我也找到了这样的答案,但他们似乎使用了不是最新的库。 – Zwirbelbart