Hi,
When I call a method(present in external DLL) through reflection which having no parameters and void return ,it get called fine but in same DLL when I call a method which have no parameters but return type Datatable then I got the 'TargetInvocation' exception.
Can anybody help me ?

Dani AI

Generated

Brief recap and practical next steps (for — and credit to for the original diagnosis).

When a method invoked by reflection throws, the runtime wraps that error in a TargetInvocationException; the real cause lives on the InnerException. The reported message points to a mismatch between the values being added and the DataTable’s column schema (not a reflection bug). The following checklist and patterns make the reflection call safer and make the real error easier to find.

  • Verify the method exists and its signature before calling (use GetMethod and check ParameterInfo/ReturnType).
  • Instantiate correctly (or handle static methods) and pass the right argument array (null or empty array for zero parameters).
  • Wrap Invoke in a try/catch for TargetInvocationException and log InnerException.Message and InnerException.StackTrace — that shows the original failure.
  • After Invoke, cast the result safely (for example as DataTable) and validate Columns.Count before adding rows; either add the necessary DataColumn definitions or build each DataRow explicitly.

Example pattern for safe invocation and error reporting:

var mi = type.GetMethod(methodSelected, BindingFlags.Instance | BindingFlags.Public);
object instance = Activator.CreateInstance(type);
try {
  var result = mi.Invoke(instance, null);
  var table = result as DataTable;
  // validate table and its Columns before mutating
}
catch (TargetInvocationException tie) {
  var original = tie.InnerException ?? tie;
  // log original.Message and original.StackTrace for debugging
}

Extra suggestions: run the external method directly (not via reflection) in a small test project to reproduce the error with normal stack traces; consider a shared interface or delegate for repeated calls instead of raw reflection; and prefer returning plain DTOs when exposing plugin-like methods to reduce runtime surprises.

Recommended Answers

All 5 Replies

The TargetInvocationException tells you that the method you are calling has thrown an exception. Check the InnerException property of the exception to find out what the underlying cause might have been.

InnerException property of the exception gives
"Input array is longer than the number of columns in this table."

/***********************My Code*****************/
Datatable Mymethod()    //Method to call 
{
    Datatable dt_Temp=new Datatable();
       dt_Temp.Rows.Add(23,45);
    dt_Temp.Rows.Add(567,566);
     return dt_Temp;
}

methodSelected="MyMethod";

object ibaseObject = Activator.CreateInstance(type);
                    object result=new object(){};
        object[] arguments = new object[0];
  MethodInfo methodInfo=  type.GetMethod(methodSelected);
   result= methodInfo.Invoke(ibaseObject,arguments);

/************************End of code******************/

its the line dt_Temp.Rows.Add(23,45); that is throwing the exception. You are adding 2 values to the datatable but the datatable doesnt have 2 columns.
Try this:

Datatable dt_Temp = new DataTable();
            dt_Temp.Columns.Add(); //optionally use Columns.Add("name here") to name your columns
            dt_Temp.Columns.Add();
            dt_Temp.Rows.Add(23, 45);

Ryshad ............................
I don't have words to thanks u.
You are just awesome.
Thanks again for your so quick reply.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.