namespace CSharpReflectionSamples { using System; using System.Reflection;
/// <summary> /// Summary description for Client. /// </summary> public class Client { public static void Main() { // the typeof Operator and the GetType method // both return a 'Type' object. Type type1 = typeof(Reflect); Reflect objTest = new Reflect(0); Type type2 = objTest.GetType();
Console.WriteLine("Type of objTest is {0}", type2); Console.WriteLine(); // pause Console.ReadLine();
// reflect method information MethodInfo[] minfo = type1.GetMethods(); // iterate through methods foreach (MethodInfo m in minfo) { Console.WriteLine(m); } Console.WriteLine(); } } }
namespace CSharpReflectionSamples { using System; using System.Reflection;
/// <summary> /// Summary description for Client. /// </summary> public class Client { public static void Main() { // the typeof operator and the GetType method // both return a 'Type' object. Type type1 = typeof(Reflect); Reflect objTest = new Reflect(0); Type type2 = objTest.GetType();
Console.WriteLine("Type of objTest is {0}", type2); Console.WriteLine(); // pause Console.ReadLine();
// reflect constructors ConstructorInfo[] cinfo = type1.GetConstructors(); // iterate through constructors foreach (ConstructorInfo c in cinfo) { Console.WriteLine(c); } } } }
namespace CSharpReflectionSamples { using System; using System.Reflection;
/// <summary> /// Summary description for Client. /// </summary> public class Client { public static void Main() { // the typeof operator and the GetType method // both return a 'Type' object. Type type1 = typeof(Reflect); Reflect objTest = new Reflect(0); Type type2 = objTest.GetType();
// dynamic creation and invocation // instantiate the Reflect object, passing // a value of 1 to the constructor object[] oConstructParms = new object[] {1}; object obj = Activator.CreateInstance(type1, oConstructParms); // invoke method of reflect object object[] oMethodParms = new object[] {17}; int intResult = (int)type1.InvokeMember("AMethod", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, oMethodParms); Console.WriteLine("Result of calling AMethod on {0} is {1}", type1.Name, intResult); // pause Console.ReadLine(); } } }