Mega Code Archive

 
Categories / C# / Development Class
 

Uses reflection to execute a class method indirectly

/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Reflect2.cs -- Uses reflection to execute a class method indirectly. // //                Compile this program with the following command line: //                    C:>csc Reflect2.cs // using System; using System.Reflection; namespace nsReflect {     class clsReflection     {         private double pi = 3.14159;         public double Pi         {             get {return (pi);}         }         public string ShowPi ()         {             return ("Pi = " + pi);         }     }     public class Reflect2     {         static public void Main ()         {             clsReflection refl = new clsReflection ();             Type t = refl.GetType();             MethodInfo GetPi = t.GetMethod ("ShowPi");             Console.WriteLine (GetPi.Invoke (refl, null));         }     } }