A Place for C Sharpers/.Netters

I Will do coding till last moment of life-Kiran Patil

Archive for the ‘Reflection’ Category

Object does not match target type+Reflection

Posted by kiranpatils on September 18, 2008

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:

  1. Declare your method which you are going to invoke as Static. [Which I don’t want to use].
  2. 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

Posted in .NET, Reflection | Tagged: | 3 Comments »