Problem:
When invoking a method using MethodInfor.InvokeMemeber() Method using Reflection.
It gives “Object does not match target type“
Solution:
For solving this error you can do two things:
- Declare your method which you are going to invoke as Static. [Which I don’t want to use].
- create instance of your type using Activator.CreateInstance(TYPE) and pass it as a first argument to InvokeMember() Method.
So final code looks like this
Assembly assembly = Assembly.LoadFile(FullAssemblyPath)
Type foundType = assembly.GetType(“TYPETOGET”)
MethodInfo foundMethod = foundType.GetMethod(“METHODTOFIND”)
Object [] params = new object [] {‘A’, 1,’B’};
Object response = foundMethod.Invoke(Activator.CreateInstance(foundType ), params);
That’s it