Unable to invoking method via reflection

C

cartoper

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());
}
}
}
 
J

Jon Skeet [C# MVP]

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:

public int AddToA(int value)

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.
 
C

chris martin

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.
 
M

Mattias Sjögren

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
 

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