Hi, In this code snippet, you will be knowing how to create an object and invoke the method dynamically. Class which we are going to instantiate and invoke a method in that dynamically.
public class ReflectionClass { public int AddNumbers(int number1, int number2) { return number1 + number2; } }
To invoke the method, put the following piece of code. Make you have imported or written using for System.Reflection.
Type ReflectionObject = typeof(ReflectionClass); object classObject = Activator.CreateInstance(ReflectionObject); ReflectionObject.GetMethod("AddNumbers").Invoke(classObject, new object[] {3,4 }); Console.WriteLine(returnValue.ToString());
|
No responses found. Be the first to respond and make money from revenue sharing program.
|