i am trying to execute commands by entering a string. I have the following class:
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();
}
Strings and equivalent types are registered in a Dictionary like this:
public static Dictionary<string, Type> CommandRegister =
new Dictionary<string, Type> { {"test", typeof(TestECommand)} };
I try to test it on this class:
class TestECommand : ECommand {
public new object Execute() {
Console.WriteLine("test");
return "k";
}
}
When calling it
ECommand.TryExecute(source, out res);
i get the following exception:
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
What am I doing wrong?