Gentlemen, there is such a code:

using System; using System.Reflection; class a { public a() { Console.WriteLine(1); } public a(int x) { Console.WriteLine(x); } public a(bool b, double z) { Console.WriteLine("Bla"); } public void Z() { Console.WriteLine("Z"); } public void S(int x) { Console.WriteLine("S"); } public int Z(int z) { Console.WriteLine("int Z"); return 5; } public void S(double z) { Console.WriteLine("double s"); } } class b { static void Main() { ConstructorInfo[] ci = typeof(a).GetConstructors(); MethodInfo[] mi = typeof(a).GetMethods(); foreach (ConstructorInfo CI in ci) { ParameterInfo[] pi = CI.GetParameters(); if (pi.Length == 1 && pi[0].ParameterType.Name == typeof(int).Name) { object[] o = new object[pi.Length]; o[pi.Length - 1] = 4; foreach (MethodInfo MI in mi) { ParameterInfo[] MPI = MI.GetParameters(); if (MPI.Length == 0) { MI.Invoke(CI.Invoke(o), null); } if (MI.ReturnParameter.Name == typeof(int).Name && MPI.Length == 1) { object[] mo = new object[1] { 6 }; MI.Invoke(CI.Invoke(o), mo); } } } } } } 

Here I try how reflexive method calls and object creation work. However, I stumbled upon miracles in c #. While running the program step by step, I see how the program calls the instruction for the first time:

 if (MPI.Length == 0) { MI.Invoke(CI.Invoke(o), null); } 

Well, as a result, an object is created (the figure 4 is displayed on the console) and the corresponding method is started (which displays the letter Z). However, with further step-by-step debugging, I see how the compiler enters this instruction again, but this creates an object (and the figure 4 is output), BUT the method does NOT run. WHY?
That is, I expect to output to the console something like 4Z4Z4Z, etc. And I get 4Z444.

    1 answer 1

    Understood !!!

    I forgot that when getting the list of methods, inherited methods also go. In order to avoid it, you need to enter

     MethodInfo[] mi = typeof(a).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);