我正在尝试通过输入字符串来执行命令。我有以下课程:
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
我做错了什么?
答案 0 :(得分:1)
我认为你混淆了extern
关键字 - 它通常用于P / invoke。
我认为你的意思是abstract
public abstract object Execute();