Mega Code Archive

 
Categories / C# / Reflection
 

Invokes the method or constructor represented by the current instance, using the specified parameters

using System; using System.Reflection; public class MyClass {     private int myValue;     public MyClass()     {         myValue = 9;     }     public int SetValue(int v)     {         return v * myValue;     } } public class TestMethodInfo {     public static void Main()     {         Type magicType = Type.GetType("MyClass");         ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);         object MyClassObject = magicConstructor.Invoke(new object[]{});         MethodInfo magicMethod = magicType.GetMethod("SetValue");         object magicValue = magicMethod.Invoke(MyClassObject, new object[]{100});         Console.WriteLine(magicValue);     } }