利用反射调用方法
C1 newC1Instance = new C1(); var funM = newC1Instance.GetType().GetMethod("fun", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); object[] para = { }; if (funM == null) { Console.Write("error, funM is null"); } else funM.Invoke(newC1Instance, para); Console.Read();
public class C1 { private void fun() { Console.Write("C1 fun"); } } public class C2 { public void fun() { Console.Write("c2 fun"); } }