473,508 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MethodInfo.Invoke and TargetInvocationException

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",BindingFl ags.Static|BindingFlags.Public|BindingFlags.Flatte nHierarchy);
If I dont'use any bindign flags GetMethod return null.

Anyone can help me ?

Nov 17 '06 #1
3 3496
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
Nov 17 '06 #2
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" <la*****@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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",BindingFl ags.Static|BindingFlags.Public|BindingFlags.Flatte nHierarchy);
If I dont'use any bindign flags GetMethod return null.

Anyone can help me ?

Nov 17 '06 #3
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" <la*****@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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",BindingFl ags.Static|BindingFlags.Public|BindingFlags.Flatte nHierarchy);
If I dont'use any bindign flags GetMethod return null.

Anyone can help me ?
Nov 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
7332
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values...
0
391
by: Ffelagund | last post by:
Hello I'm trying to load dinamically some dll's but when I try to invoke one method, I ever get ArgumentException if I'm not using native types. Here are the .h of the dll: #pragma once...
1
2185
by: Sivaraman.S | last post by:
Hi, Can i pass integer array to methodInfo.Invoke(obj,args()). I am able to pass only string array to this function. This is the code i have written. Dim myType As Type = objClass.GetType()...
3
9534
by: Me | last post by:
I am trying to figure out any issues with calling System.Reflection.MethodInfo.Invoke() when dealing with multiple threads. For instance.. Say I have a class that allows you to pass in a...
2
12246
by: Phill W. | last post by:
For some time, I've been adding "trace level" logging to my code using Imports System.Reflection Private Function XYZ( a1 as A, b1 as B, c1 as C, ... TRACE.Log(...
1
12675
by: ian | last post by:
Hi, I can't find a solution to this, so I've brought it to the experts. Using reflection I can get a MethodInfo object pointing at an assembly's method. Where I have a MethodInfo object...
2
3018
by: John Lutz | last post by:
Our app is mainly COM based. We allow .NET plug-ins. When a plug-in is installed, it receives a COM pointer to the app. The plug-in implements our IExtensionImpl interface and gets the app...
3
3404
by: Lambuz | last post by:
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...
15
8170
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
11
23611
by: KMinFL | last post by:
This is a C# VS 2008 question... Our system has 2 base classes, SingleEntity and NewPluralEntity. SingleEntity provides access to properties and methods related to manipulating data in a database...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.