mercredi 23 janvier 2019

"Object does not match target type" when trying to call generic method

I try to call a generic method by Type following this answer:

public void CallByModule(string moduleName, int id, int userId) {
  MethodInfo method = GetMethod<ModuleBaseLogic>(q => q.Like<object>(id, userId));
  Type t=this.Assembly.GetType(this.Assembly.Namespace+".Modules."+moduleName);
  MethodInfo genericMethod = method.MakeGenericMethod(t);
  genericMethod.Invoke(this, null);
}

(ModuleBaseLogic is the class containing the "Like(int,int)"-Function)

The Type seems to be a valid Type and the generic Method also seems to be valid: {Void Like[InnovationIdea](Int32, Int32)} is being found (InnovationIdea is the (correctly detected) generic Type)

The Method header is

 void Like<T>(int id, int userId);

On the "Invoke" I receive the following Exception:

TargetException: Object does not match target type.

What am I doing wrong here?





Aucun commentaire:

Enregistrer un commentaire