1

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?

4

1 回答 1

1

我认为您混淆extern了关键字-它通常用于 P/invoke。

我想你的意思是abstract

public abstract object Execute();
于 2013-06-02T00:04:19.753 回答