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