public static void Main() { var constructors = typeof(BlackBoxInteger) .GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance); BlackBoxInteger blackBoxInteger = (BlackBoxInteger)constructors[0].Invoke(new object[] { 0 }); while (true) { string[] input = Console.ReadLine().Split('_'); string methodName = input[0]; if (methodName == "END") { break; } int value = int.Parse(input[1]); MethodInfo method = blackBoxInteger .GetType() .GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(blackBoxInteger, new object[] { value }); FieldInfo innerValueField = blackBoxInteger .GetType() .GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance); Console.WriteLine(innerValueField.GetValue(blackBoxInteger)); } }
private static void Caller(string command, int value, BlackBoxInteger blackBoxInt) { var method = blackBoxInt .GetType() .GetMethod(command, BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(blackBoxInt, new object[] { value }); var innerValue = blackBoxInt .GetType() .GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic); Console.WriteLine(innerValue.GetValue(blackBoxInt)); }
public static void Main() { string input = string.Empty; ConstructorInfo blackBoxIntConstructor = (typeof(BlackBoxInteger).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(int) }, null)); BlackBoxInteger blackBoxInteger = blackBoxIntConstructor.Invoke(new object[] { 0 }) as BlackBoxInteger; MethodInfo[] blackBoxIntMethods = blackBoxInteger.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance); while ((input = Console.ReadLine()) != "END") { string[] inputs = input.Split(new char[] { '_' }); FieldInfo[] blackBoxFields = blackBoxInteger.GetType().GetFields(); string methodName = inputs[0]; int parameter = int.Parse(inputs[1]); MethodInfo methodToInvoke = blackBoxIntMethods.Where(m => m.Name == methodName).First(); methodToInvoke.Invoke(blackBoxInteger, new object[] { parameter }); FieldInfo innerIntField = blackBoxInteger.GetType().GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance); int result = (int)(innerIntField.GetValue(blackBoxInteger)); Console.WriteLine(result); } }
private static void ProcessCommands(BlackBoxInteger instance, Dictionary <string, MethodInfo> methods) { string inputLine; var innerValueField = instance.GetType() .GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public) .FirstOrDefault(); while ((inputLine = Console.ReadLine()) != "END") { var tokens = inputLine.Split("_"); var command = tokens[0]; var argument = int.Parse(tokens[1]); var currentMethod = methods[command]; currentMethod.Invoke(instance, new object[] { argument }); Console.WriteLine(innerValueField.GetValue(instance)); } }
public ComandInterpreter(BlackBoxInteger blackBoxInteger) { this.blackBoxInteger = blackBoxInteger; this.methods = blackBoxInteger.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); }