MethodInfo.Invoke and TargetInvocationException

L

Lambuz

Hi all,
when I use MethosInfo.Invoke method I obtain a
TargetInvocationException where inner exception is
+{"classBase.OpenConn cannot be invoked directly" } System.Exception.

I'm using the following class

public abstract class classBase{
public static int OpenConn(){
....
}
}

public abstract class classDerived: classBase{

}

In my code I'm obtaining the info about OpenConn method starting from
the type of classDerived by using this code:
methodInfo =
classDerivedType.GetMethod("OpenArchive",BindingFlags.Static|BindingFlags.Public|BindingFlags.FlattenHierarchy);


If I dont'use any bindign flags GetMethod return null.

Anyone can help me ?
 
M

Marc Gravell

If the method is static, then it isn't really participating in the
inheritance, so you may as well execute it directly as classBase.OpenConn().
I'm assuming the OpenArchive is a copy/paste/hack. But either just use the
static method, or get the method info from the base type...
(typeof(classBase).GetMethod(...)) if you *really* need it as a MethodInfo.

Marc
 
D

Dave Sexton

Hi,

"OpenArchive" isn't declared in your example.

If it's an instance method, it won't work. You can't invoke a method on an
abstract class since there is no implementation to be invoked.

If it's a static method the following code should work fine:

object[] args = null; // use null if there is no arguments
methodInfo.Invoke(null, args);
 
L

Lambuz

Thanks to all, I've solved alone my problem.

I was making a silly coding error.

thanks for the help

Dave Sexton ha scritto:
Hi,

"OpenArchive" isn't declared in your example.

If it's an instance method, it won't work. You can't invoke a method on an
abstract class since there is no implementation to be invoked.

If it's a static method the following code should work fine:

object[] args = null; // use null if there is no arguments
methodInfo.Invoke(null, args);

--
Dave Sexton

Lambuz said:
Hi all,
when I use MethosInfo.Invoke method I obtain a
TargetInvocationException where inner exception is
+{"classBase.OpenConn cannot be invoked directly" } System.Exception.

I'm using the following class

public abstract class classBase{
public static int OpenConn(){
...
}
}

public abstract class classDerived: classBase{

}

In my code I'm obtaining the info about OpenConn method starting from
the type of classDerived by using this code:
methodInfo =
classDerivedType.GetMethod("OpenArchive",BindingFlags.Static|BindingFlags.Public|BindingFlags.FlattenHierarchy);


If I dont'use any bindign flags GetMethod return null.

Anyone can help me ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top