static void Main() { Type classType = typeof(BlackBoxInt); BlackBoxInt blackBox = (BlackBoxInt)Activator.CreateInstance(classType, true); //First way ConstructorInfo methodInfo = classType.GetConstructor //Second way (BindingFlags.Instance | BindingFlags.NonPublic, Type.DefaultBinder, new Type[] { }, null); string inputLine; while ((inputLine = Console.ReadLine()) != "END") { string[] tokens = inputLine.Split('_'); string methodName = tokens[0]; int value = int.Parse(tokens[1]); classType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(blackBox, new object[] { value }); object innerStateValue = classType .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First() .GetValue(blackBox); Console.WriteLine(innerStateValue); } }
public static void Main(string[] args) { Type classType = typeof(BlackBoxInt); //ConstructorInfo blackBoxCtor = classType.GetConstructor( //BindingFlags.Instance | BindingFlags.NonPublic, Type.DefaultBinder, new Type[] {}, null); // I -way -> Take the empty constr of class BlackBoxInt. BlackBoxInt blackBoxCtor = (BlackBoxInt)Activator.CreateInstance(classType, true); // II-way -> take emty ctor of class BlackBoxInt. string input; while ((input = Console.ReadLine()) != "END") { var tokens = input.Split('_'); var methodName = tokens[0]; var value = int.Parse(tokens[1]); classType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic).Invoke(blackBoxCtor, new object[] { value }); //Call current method and invoke him with current value //Take value of field after above method invoked. string innerStateValue = classType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First() .GetValue(blackBoxCtor) .ToString(); Console.WriteLine(innerStateValue); } }
public string Run() { Type classType = Type.GetType("BlackBoxInt"); BlackBoxInt blackBoxInstance = (BlackBoxInt)Activator.CreateInstance(classType, true); MethodInfo[] classMethods = classType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); FieldInfo[] classFields = classType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); string input = string.Empty; while (!(input = Console.ReadLine()).Equals("END")) { string[] cmdArgs = input.Split('_'); string command = cmdArgs[0]; int number = int.Parse(cmdArgs[1]); MethodInfo currentMethod = classMethods.FirstOrDefault(m => m.Name.Equals(command)); if (currentMethod != null) { currentMethod.Invoke(blackBoxInstance, new object[] { number }); } foreach (FieldInfo field in classFields) { this.sb.AppendLine(field.GetValue(blackBoxInstance).ToString()); } } return(this.sb.ToString().Trim()); }
static void Main(string[] args) { Type classType = typeof(BlackBoxInt); ConstructorInfo ctorInfo = classType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, new[] { typeof(int) }, null); BlackBoxInt blackBox = (BlackBoxInt)ctorInfo.Invoke(new object[] { 0 }); MethodInfo[] methods = classType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); string input = string.Empty; MethodInfo currentMethod = null; while ((input = Console.ReadLine()) != "END") { string[] tokens = input.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries); string methodName = tokens[0]; int inputValue = int.Parse(tokens[1]); currentMethod = classType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); currentMethod.Invoke(blackBox, new object[] { inputValue }); FieldInfo innerValue = classType.GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic); Console.WriteLine(innerValue.GetValue(blackBox)); } }
static void Main() { Type myType = typeof(BlackBoxInt); FieldInfo[] allFields = myType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic); FieldInfo field = allFields.First(f => f.Name == "innerValue"); ConstructorInfo[] nonPublicCtors = myType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic); ConstructorInfo ourConstructor = nonPublicCtors[0]; BlackBoxInt testBlackBox = (BlackBoxInt)ourConstructor.Invoke(new object[] { 0 }); MethodInfo[] methods = myType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); string input = Console.ReadLine(); while (input != "END") { string[] commandInfo = input.Split('_'); object[] parameters = new object[] { int.Parse(commandInfo[1]) }; switch (commandInfo[0]) { case "Add": MethodInfo addMethod = methods.First(m => m.Name == "Add"); addMethod.Invoke(testBlackBox, parameters); break; case "Subtract": MethodInfo subtractMethod = methods.First(m => m.Name == "Subtract"); subtractMethod.Invoke(testBlackBox, parameters); break; case "Divide": MethodInfo divideMethod = methods.First(m => m.Name == "Divide"); divideMethod.Invoke(testBlackBox, parameters); break; case "Multiply": MethodInfo multiplyMethod = methods.First(m => m.Name == "Multiply"); multiplyMethod.Invoke(testBlackBox, parameters); break; case "RightShift": MethodInfo rightShiftMethod = methods.First(m => m.Name == "RightShift"); rightShiftMethod.Invoke(testBlackBox, parameters); break; case "LeftShift": MethodInfo leftShiftMethod = methods.First(m => m.Name == "LeftShift"); leftShiftMethod.Invoke(testBlackBox, parameters); break; } Console.WriteLine(field.GetValue(testBlackBox)); input = Console.ReadLine(); } }
static void Main(string[] args) { Type classType = typeof(BlackBoxInt); ConstructorInfo constructor = classType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First(); BlackBoxInt classInstance = (BlackBoxInt)constructor.Invoke(new object[] { 0 }); FieldInfo containerField = classType.GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic); MethodInfo addMethod = classType.GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo subtractMethod = classType.GetMethod("Subtract", BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo divideMethod = classType.GetMethod("Divide", BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo multiplyMethod = classType.GetMethod("Multiply", BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo shiftRightMethod = classType.GetMethod("RightShift", BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo shiftLeftMethod = classType.GetMethod("LeftShift", BindingFlags.NonPublic | BindingFlags.Instance); var input = Console.ReadLine(); while (input != "END") { var splitted = input.Split('_').ToList(); string command = splitted[0]; int value = int.Parse(splitted[1]); switch (command) { case "Add": addMethod.Invoke(classInstance, new object[] { value }); break; case "Subtract": subtractMethod.Invoke(classInstance, new object[] { value }); break; case "Divide": divideMethod.Invoke(classInstance, new object[] { value }); break; case "Multiply": multiplyMethod.Invoke(classInstance, new object[] { value }); break; case "RightShift": shiftRightMethod.Invoke(classInstance, new object[] { value }); break; case "LeftShift": shiftLeftMethod.Invoke(classInstance, new object[] { value }); break; } Console.WriteLine(containerField.GetValue(classInstance)); input = Console.ReadLine(); } }
static void Main() { string inputLine; Type classType = Type.GetType("BlackBoxInt"); ConstructorInfo ctor = classType .GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, new[] { typeof(int) }, null); BlackBoxInt box = (BlackBoxInt)ctor .Invoke(new object[] { 0 }); while ((inputLine = Console.ReadLine()) != "END") { string[] tokens = inputLine.Split('_'); MethodInfo classMethod = classType .GetMethod(tokens[0], BindingFlags.Instance | BindingFlags.NonPublic); classMethod.Invoke(box, new object[] { int.Parse(tokens[1]) }); FieldInfo value = classType .GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic); Console.WriteLine(value.GetValue(box)); } }
public static void Main() { string input = Console.ReadLine(); BlackBoxInt integer = new BlackBoxInt(); var type = typeof(BlackBoxInt); var field = type.GetField("value", BindingFlags.NonPublic | BindingFlags.Instance); while (input != "END") { string[] data = input.Split('_'); var methodInfo = type.GetMethod(data[0], BindingFlags.Instance | BindingFlags.NonPublic); methodInfo.Invoke(integer, new object[] { int.Parse(data[1]) }); int number = (int)field.GetValue(integer); Console.WriteLine(number); input = Console.ReadLine(); } }
public static void Main(string[] args) { Type blackBoxType = typeof(BlackBoxInt); BlackBoxInt blackBox = (BlackBoxInt)Activator.CreateInstance(blackBoxType, true); string input; while ((input = Console.ReadLine()) != "END") { string[] tokens = input.Split('_'); string methodName = tokens[0]; int value = int.Parse(tokens[1]); blackBoxType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(blackBox, new object[] { value }); object innerStateValue = blackBoxType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First() .GetValue(blackBox) .ToString(); Console.WriteLine(innerStateValue); } }
public static void Main() { Type blackBoxIntClassType = typeof(BlackBoxInt); BlackBoxInt blackBoxIntClassInstance = (BlackBoxInt)Activator.CreateInstance(blackBoxIntClassType, true); string command = Console.ReadLine(); while (command != "END") { string[] commandArgs = command.Split('_'); string commandType = commandArgs[0]; object commandValue = int.Parse(commandArgs[1]); MethodInfo targetMethod = blackBoxIntClassType.GetMethod(commandType, BindingFlags.NonPublic | BindingFlags.Instance); targetMethod.Invoke(blackBoxIntClassInstance, new object[] { commandValue }); FieldInfo valueField = blackBoxIntClassType.GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance); Console.WriteLine(valueField.GetValue(blackBoxIntClassInstance)); command = Console.ReadLine(); } }