471,354 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 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 6421
<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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nelson P. Varghese | last post: by
1 post views Thread by Beenish Sahar Khan | last post: by
3 posts views Thread by Rotsey | last post: by
reply views Thread by =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.