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.Add ToA' not found.
Here is the code:
using System;
using System.Diagnost ics;
using System.Collecti ons.Generic;
using System.Reflecti on;
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.Pu blic |
BindingFlags.In vokeMethod |
BindingFlags.In stance;
object result = obj.GetType().I nvokeMember(
"AddToA", flags, null, obj, new object[] { "5" });
Debug.WriteLine (result.ToStrin g());
}
}
}
methods via reflection using the InvokeMember method. The InvokeMember
method throws an exception:
Method 'ReflectionTest .Originator.Add ToA' not found.
Here is the code:
using System;
using System.Diagnost ics;
using System.Collecti ons.Generic;
using System.Reflecti on;
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.Pu blic |
BindingFlags.In vokeMethod |
BindingFlags.In stance;
object result = obj.GetType().I nvokeMember(
"AddToA", flags, null, obj, new object[] { "5" });
Debug.WriteLine (result.ToStrin g());
}
}
}
Comment