/* * Execute private methods: * Written by: Eber Irigoyen * http://ebersys.blogspot.com * on: 10/23/2006 * License: LGPL * Description: Muestra un metodo para ejecutar metodos privados de una clase */ using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Diagnostics; namespace ExecutePrivateMethods { public class TestClass { public TestClass() { } private void HiddenMethod() { Console.WriteLine("secret method, should only be called from the containing class itself"); } private string HiddenMethodWithParams(string firstName, int age) { return string.Format("hello {0}, you are {1} years old", firstName, age); } public static void Test() { } } class Program { static void Main(string[] args) { TestClass tc = new TestClass(); Type testClaseType = typeof(TestClass); MethodInfo hiddenMethod = testClaseType.GetMethod("HiddenMethod", BindingFlags.NonPublic | BindingFlags.Instance);//, //null, new Type[0] { }, null); hiddenMethod.Invoke(tc, null); MethodInfo hiddenMethodWithParams = testClaseType.GetMethod("HiddenMethodWithParams", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[2] { typeof(string), typeof(int) }, null); string result = (string)hiddenMethodWithParams.Invoke(tc, new object[2] { "Eber", 25 }); Console.WriteLine("result: "+ result); Console.Read(); } } }