Skip to content
September 18, 2008 / kiranpatils

Object does not match target type+Reflection

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

8 Comments

Leave a Comment
  1. Sres / Mar 17 2009 11:31 am

    Thanks for posting this, you help assist in solving my binding issue. Great work!

  2. Ali / Apr 30 2009 6:40 am

    hi
    i don’t know what should i put instead of “TYPETOGET” parameter

    • kiranpatils / May 4 2009 1:46 am

      Hello,

      Thanks for visiting my blog..

      TYPETOGET = Name of type which you want to get…e.g Employee..so if i give you full example then it will look like this:

      Assembly assembly = Assembly.LoadFile(FullAssemblyPath)

      Type foundType = assembly.GetType(Employee)

      MethodInfo foundMethod = foundType.GetMethod(AddEmployee)

      Object [] params = new object [] {‘A’, 1,’B’};

      Object response = foundMethod.Invoke(Activator.CreateInstance(foundType ), params);

      //Above code will Create Instance of Type Employee and Call its AddEmployee Method..

      Let me know if you are not clear…

      HTH
      -Kiran

  3. Renato Person / Feb 22 2010 4:59 pm

    Wonderful… I was broking my head with this error. Thanks!

  4. veerabhadra / Mar 7 2012 6:17 am

    public static DataSet ToDataSet(this IList list)
    {

    Type elementType = typeof(T);
    DataSet ds = new DataSet();
    DataTable t = new DataTable();

    //add a column to table for each public property on T
    foreach (var propInfo in elementType.GetProperties())
    {
    if (propInfo.PropertyType.FullName != “System.String” && propInfo.PropertyType.IsClass == true)
    {
    Type ele = propInfo.PropertyType;
    foreach (var propInfo1 in ele.GetProperties())
    {
    t.Columns.Add(propInfo1.Name);//, propInfo1.PropertyType);
    }

    }
    else
    {
    t.Columns.Add(propInfo.Name);//, propInfo.PropertyType);
    }
    }

    //go through each property on T and add each value to the table
    foreach (T item in list)
    {
    DataRow row = t.NewRow();
    foreach (var propInfo in elementType.GetProperties())
    {
    if (propInfo.PropertyType.FullName != “System.String” && propInfo.PropertyType.IsClass == true)
    {
    Type ele = propInfo.PropertyType;

    foreach (var propInfo1 in ele.GetProperties())
    {
    //here i get an error becoze of proparty dose’t match bcz it contain inner object
    row[propInfo1.Name] = propInfo1.GetValue(item,null);
    }

    }
    else
    {
    row[propInfo.Name] = propInfo.GetValue(item, null);
    }
    }

    //This line was missing:
    t.Rows.Add(row);
    }

    ds.Tables.Add(t);
    return ds;
    }

    plz help me i want to extract form list its contain inner object i got first parent object data but when its child object extract getvalue it give an error

  5. Novo / Nov 30 2013 6:33 am

    We enjoy, result in I came across just what I was looking for. You could have concluded my own Some working day extended quest! Lord Cheers guy. Have a wonderful time. Cya

  6. Jia / Feb 14 2014 5:00 pm

    Thanks. This Helped.

  7. support.cameracity.com.au / May 17 2014 2:31 am

    Good blog you have got here.. It’s difficult
    to find excellent writing like yours these
    days. I seriously appreciate individuals like you! Take care!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: