mardi 29 mai 2018

Cannot get Method from assembly at runtime

I'm using the following code to load an assembly at runtime and then get a reference to a specific method and obviously execute it at the end:

var assemblyLoaded = Assembly.LoadFile(absolutePath);
var type = assemblyLoaded.GetType("CreateContactPlugin.Plugin");

var instance = Activator.CreateInstance(type);

var methodInfo = type.GetMethod("Execute", new Type[] { typeof(System.String)});
if (methodInfo == null)
{
    throw new Exception("No such method exists.");
}

Here is the assembly that I'm calling

namespace CreateContactPlugin
{
   public class Plugin
   {

    static bool Execute(string contactName){
        bool contactCreated = false;
        if (!String.IsNullOrWhiteSpace(contactName))
        {
            //process
        }
        return contactCreated;
    }

  }
 }

I can succesfully load the Assembly, the Type. When I highlight the type variable, I see the method listed in the DeclaredMethods array. But when I try to get the Method, it returns always null.

Does somebody see what I might be doing wrong here ?





Aucun commentaire:

Enregistrer un commentaire