public string Run() { Type type = typeof(BlackBoxInteger); FieldInfo[] fields = type.GetFields(flagsForFields); MethodInfo[] methods = type.GetMethods(flagsForMethods); BlackBoxInteger instance = (BlackBoxInteger)Activator.CreateInstance(type, true); string input = Console.ReadLine(); while (input != "END") { string[] tokens = input.Split('_'); string command = tokens[0]; int value = int.Parse(tokens[1]); methods.FirstOrDefault(method => method.Name == command)?.Invoke(instance, new object[] { value }); foreach (var field in fields) { this.sb.AppendLine(field.GetValue(instance).ToString()); } input = Console.ReadLine(); } return(sb.ToString().TrimEnd()); }
private static void Main() { Type type = typeof(BlackBoxInteger); BlackBoxInteger classInstance = (BlackBoxInteger)Activator.CreateInstance(type, true); string command = Console.ReadLine(); while (command != "END") { string[] items = command.Split("_"); string methodName = items[0]; int value = int.Parse(items[1]); MethodInfo methods = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic); methods.Invoke(classInstance, new object[] { value }); object result = type.GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(classInstance); Console.WriteLine(result); command = Console.ReadLine(); } }
private static void ProcessMethod(Type blackBoxType, BlackBoxInteger classInstance, string fieldName, string methodName, int number) { var method = blackBoxType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(classInstance, new object[] { number }); PrintFieldValue(blackBoxType, classInstance, fieldName); }
public static void Main() { Type type = typeof(BlackBoxInteger); FieldInfo valueField = type.GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance); ConstructorInfo[] constrctors = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance); BlackBoxInteger blackBoxInteger = constrctors[1].Invoke(new object[] { }) as BlackBoxInteger; string command = ""; while ((command = Console.ReadLine()) != "END") { string[] data = command.Split("_"); string methodName = data[0]; int value = int.Parse(data[1]); MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(blackBoxInteger, new object[] { value }); Console.WriteLine(valueField.GetValue(blackBoxInteger)); } }
static void Main() { type = typeof(BlackBoxInteger); instance = (BlackBoxInteger)Activator.CreateInstance(type, true); MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic); while (true) { string input = Console.ReadLine(); if (input == "END") { return; } string command = input.Substring(0, input.IndexOf("_")); string argument = input.Substring(input.IndexOf("_") + 1); Console.WriteLine(MethodInvoker(command, argument)); } }
public static void Main() { Type type = typeof(BlackBoxInteger); BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; ConstructorInfo constructor = type.GetConstructor(flags, null, new Type[] { typeof(int) }, null); BlackBoxInteger instance = (BlackBoxInteger)constructor.Invoke(new object[] { 0 }); FieldInfo field = type.GetField("innerValue", flags); string command; while ((command = Console.ReadLine()) != "END") { var data = command.Split('_'); var methodName = data[0]; var number = int.Parse(data[1]); MethodInfo method = type.GetMethod(methodName, flags); method.Invoke(instance, new object[] { number }); Console.WriteLine(field.GetValue(instance)); } }
private static void PrintFieldValue(Type blackBoxType, BlackBoxInteger classInstance, string fieldName) { Console.WriteLine(blackBoxType.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(classInstance)); }