public static void Main() { Type blackboxType = typeof(BlackBoxInt); BlackBoxInt blackboxInstance = (BlackBoxInt)Activator.CreateInstance(blackboxType, true); Console.WriteLine(blackboxInstance.GetType()); //ConstructorInfo constructorInfo = blackBoxType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic // ,Type.DefaultBinder,new Type[] { }, null); //constructorInfo.Invoke while (true) { var input = Console.ReadLine(); if (input == "END") { break; } var methodTokens = input.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries); var methodName = methodTokens[0]; var methodParam = int.Parse(methodTokens[1]); blackboxType .GetMethod(methodName, Flags) .Invoke(blackboxInstance, new object[] { methodParam }); var innerValue = blackboxType .GetFields(Flags) .First() .GetValue(blackboxInstance); Console.WriteLine(innerValue); } }
private static string GetInnerValue(BlackBoxInt blackBox, string method, int value) { MethodInfo getMethod = typeof(BlackBoxInt).GetMethod(method, BindingFlags.Instance | BindingFlags.NonPublic); getMethod.Invoke(blackBox, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { value }, null); return(typeof(BlackBoxInt).GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(blackBox).ToString()); }
public static void Main() { BlackBoxInt blackBox = (BlackBoxInt)Activator.CreateInstance(typeof(BlackBoxInt), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[0], null); string command; while ((command = Console.ReadLine()) != "END") { string[] tokens = command.Split('_'); string method = tokens[0]; int value = int.Parse(tokens[1]); Console.WriteLine(GetInnerValue(blackBox, method, value)); } }