简体   繁体   中英

System.TypeLoadException was unhandled: Could not load type because the method has no implementation (no RVA)

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?

I think you are confusing extern keyword - its generally used for P/invoke.

I think you meant abstract

public abstract object Execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM