473,386 Members | 1,786 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,386 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 6540
<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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...

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.