473,220 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,220 software developers and data experts.

Unable to invoking method via reflection

I am currently doing some R&D. The objective is to learn how to invoke
methods via reflection using the InvokeMember method. The InvokeMember
method throws an exception:

Method 'ReflectionTest.Originator.AddToA' not found.

Here is the code:

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace ReflectionTest
{
public class Originator
{
private int a;

public Originator(int a)
{
this.a = a;
}

public int AddToA(int value)
{
a += value;
return a;
}
}

public class Program
{
static void Main(string[] args)
{
object obj = new Originator(5);

BindingFlags flags =
BindingFlags.Public |
BindingFlags.InvokeMethod |
BindingFlags.Instance;

object result = obj.GetType().InvokeMember(
"AddToA", flags, null, obj, new object[] { "5" });

Debug.WriteLine(result.ToString());
}
}
}

Jan 24 '06 #1
3 6525
<ca******@gmail.com> wrote:
I am currently doing some R&D. The objective is to learn how to invoke
methods via reflection using the InvokeMember method. The InvokeMember
method throws an exception:
<snip>
public int AddToA(int value)
<snip>
object result = obj.GetType().InvokeMember(
"AddToA", flags, null, obj, new object[] { "5" });


You're effectively trying to call AddToA("5").

"5" is not an int, it's a string. If you change "5" to 5, the code
works.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 24 '06 #2
> I am currently doing some R&D. The objective is to learn how to
invoke methods via reflection using the InvokeMember method. The
InvokeMember method throws an exception:

Method 'ReflectionTest.Originator.AddToA' not found.

Here is the code:

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace ReflectionTest
{
public class Originator
{
private int a;
public Originator(int a)
{
this.a = a;
}
public int AddToA(int value)
{
a += value;
return a;
}
}
public class Program
{
static void Main(string[] args)
{
object obj = new Originator(5);
BindingFlags flags =
BindingFlags.Public |
BindingFlags.InvokeMethod |
BindingFlags.Instance;
object result = obj.GetType().InvokeMember(
"AddToA", flags, null, obj, new object[] { "5" });
Debug.WriteLine(result.ToString());
}
}
}

Changing the line:
object result = obj.GetType().InvokeMember(
"AddToA", flags, null, obj, new object[] { "5" });

to
object result = obj.GetType().InvokeMember(
"AddToA", flags, null, obj, new object[] { 5 });

should do it. I didn't test it though.
Jan 24 '06 #3
>Method 'ReflectionTest.Originator.AddToA' not found.
....
object result = obj.GetType().InvokeMember(
"AddToA", flags, null, obj, new object[] { "5" });


The argument you're providing is a string so it will look for a
AddToA(string) method, not your AddToA(int). Try changing "5" to 5.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 24 '06 #4

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

Similar topics

2
by: Carlos G Benevides | last post by:
I have a ASP.Net web application that has two assemblies that run under com+. Under Windows 2000 the two assemblies are added to com+ automatically when instantiated from the web site. For this...
3
by: Ben Fidge | last post by:
Hi Is it possible, using Reflection, to determine at runtime the method(s) that are provided as handler for a given controls events? Also, once discovered, is it possible to manually invoke...
2
by: Nelson P. Varghese | last post by:
Please have a look at the code given below. The class named 'Class1' has two methods with the same name but with different case. Now how can I invoke these methods from VB. using System; ...
1
by: Beenish Sahar Khan | last post by:
I'm using reflection to invoke some methods, now the general patteren of the function defination is private void FunctionName( argument1 , argument2) for some functions i don't need the...
2
by: Jeff | last post by:
I am trying to dynamically load an assembly via reflection and then invoke a method of that assembly that will populate a custom type collection passed into the method byref. I am able to...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
4
by: Gustavo Arriola | last post by:
Hello to all! I am trying to accede to a method of a class invoking(the class) from a variable string. The class is in other assembly called "infoInsumo" The code: Dim Tipo As Type =...
3
by: Rotsey | last post by:
Hi, I am getting a Exception has been thrown by the target of an invocation error when invoking a method Here is the crux of the code. I realise it could be a few things, any one give me...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.