public static void Main() { Type blackBoxType = typeof(BlackBoxInt); //DIRECT VARIANT A: BlackBoxInt myBlackBox = (BlackBoxInt)Activator .CreateInstance(blackBoxType, true); //here "true" is for NON Public ctor and create direct istance! //Variant B with ctor: //ConstructorInfo blackBoxCtor = blackBoxType.GetConstructor // (BindingFlags.Instance | BindingFlags.NonPublic, Type.DefaultBinder, // new Type[]{}, null); //var istance = (BlackBoxInt)blackBoxCtor.Invoke(new object[0]); //istance = blackBox string inputLine; while ((inputLine = Console.ReadLine()) != "END") { string[] tokens = inputLine.Split('_'); string methodName = tokens[0]; int value = int.Parse(tokens[1]); blackBoxType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(myBlackBox, new object[] { value }); string innerStateValue = blackBoxType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First() .GetValue(myBlackBox) .ToString(); Console.WriteLine(innerStateValue); } }
public static void Main(string[] args) { Type blackBoxType = typeof(BlackBoxInt); BlackBoxInt blackBox = (BlackBoxInt)Activator.CreateInstance(blackBoxType, true); //ConstructorInfo ci = blackBoxType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, Type.DefaultBinder, new Type[]{}, null); string input; while ((input = Console.ReadLine()) != "END") { string[] elements = input.Split('_'); string methodName = elements[0]; int value = int.Parse(elements[1]); blackBoxType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(blackBox, new object[] { value }); var field = blackBoxType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).First() .GetValue(blackBox); Console.WriteLine(field); } }
public static void Main() { Type blackBoxIntType = typeof(BlackBoxInt); BlackBoxInt myBlackBox = (BlackBoxInt)Activator.CreateInstance(blackBoxIntType, true); FieldInfo[] fields = blackBoxIntType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic); string fieldName = fields.FirstOrDefault().Name; string inputRequest; while ((inputRequest = Console.ReadLine()) != "END") { string[] tokens = inputRequest.Split('_'); string methodName = tokens[0]; int value = int.Parse(tokens[1]); blackBoxIntType .GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(myBlackBox, new object[] { value }); string innerState = blackBoxIntType .GetField( fieldName, BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(myBlackBox).ToString(); Console.WriteLine(innerState); } }
static void Main(string[] args) { StringBuilder sb = new StringBuilder(); Type type = typeof(BlackBoxInt); BlackBoxInt instance = (BlackBoxInt)Activator.CreateInstance(type, true); MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); string[] command = Console.ReadLine().Split('_'); while (command[0] != "END") { sb.Clear(); int value = int.Parse(command[1]); methods.FirstOrDefault(m => m.Name == command[0])?.Invoke(instance, new object[] { value }); foreach (var field in fields) { sb.AppendLine(field.GetValue(instance).ToString()); } Console.WriteLine(sb.ToString().Trim()); command = Console.ReadLine().Split('_'); } }
public static void Main() { Type blackBoxType = typeof(BlackBoxInt); BlackBoxInt myBlackBox = (BlackBoxInt)Activator.CreateInstance(blackBoxType, true); //ConstructorInfo blackBoxCtor = blackBoxType.GetConstructor(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]); blackBoxType.GetMethod(methodName, NonPulbicFlags) .Invoke(myBlackBox, new object[] { value }); object innerStateValue = blackBoxType .GetFields(NonPulbicFlags) .First() .GetValue(myBlackBox); Console.WriteLine(innerStateValue); } }
static void Main(string[] args) { Type classType = typeof(BlackBoxInt); ConstructorInfo constructor = classType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(int) }, null); BlackBoxInt boxInt = (BlackBoxInt)constructor.Invoke(new object[] { 0 }); while (true) { var inputCommands = Console.ReadLine().Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries); switch (inputCommands[0]) { case "Add": case "Subtract": case "Divide": case "Multiply": case "RightShift": case "LeftShift": Console.WriteLine(ExecuteMethod(boxInt, inputCommands[0], int.Parse(inputCommands[1]))); break; case "END": return; } } }
static void Main() { Type blackBoxType = typeof(BlackBoxInt); ConstructorInfo constructor = blackBoxType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); BlackBoxInt box = (BlackBoxInt)constructor.Invoke(null); string[] inputLine = Console.ReadLine().Split(new[] { '_' }); while (inputLine[0] != "END") { string operation = inputLine[0]; int num = int.Parse(inputLine[1]); MethodInfo blakcBoxMethod = blackBoxType.GetMethod(operation, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(int) }, null); blakcBoxMethod.Invoke(box, new object[] { num }); FieldInfo blackBoxTypeField = blackBoxType.GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance); int value = (int)blackBoxTypeField.GetValue(box); Console.ForegroundColor = ConsoleColor.DarkCyan; Console.WriteLine(value); Console.ForegroundColor = ConsoleColor.Gray; inputLine = Console.ReadLine().Split(new[] { '_' }); } }
static void Main(string[] args) { var type = typeof(BlackBoxInt); BlackBoxInt box = (BlackBoxInt)Activator.CreateInstance(type, true); // Return Default ctor //ConstructorInfo ctroBox = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,Type.DefaultBinder,new Type[]{},null ); string inputInfo; while ((inputInfo = Console.ReadLine()) != "END") { var split = inputInfo.Split('_'); var command = split[0]; var intParam = int.Parse(split[1]); var method = type.GetMethod(command, BindingFlags.Instance | BindingFlags.NonPublic).Invoke(box, new object[] { intParam }); var field = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).First().GetValue(box); Console.WriteLine(field); } }
private static int ExecuteMethod(BlackBoxInt boxInt, string inputCommand, int num) { MethodInfo method = boxInt.GetType().GetMethod(inputCommand, BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(boxInt, new object[] { num }); FieldInfo value = boxInt.GetType().GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic); return((int)value.GetValue(boxInt)); }
private static string GetInnerValue(BlackBoxInt blackBox, string[] cmdArgs) { // Find method MethodInfo methodInfo = typeof(BlackBoxInt).GetMethod(cmdArgs[0], BindingFlags.NonPublic | BindingFlags.Instance); // Invoke method methodInfo.Invoke(blackBox, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { int.Parse(cmdArgs[1]) }, null); // Get new int inner value return(typeof(BlackBoxInt).GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(blackBox).ToString()); }
static void Main(string[] args) { string input = Console.ReadLine(); StringBuilder builder = new StringBuilder(); Type classType = typeof(BlackBoxInt); Type[] ctorParamsType = { typeof(int) }; ConstructorInfo ctorInt = classType.GetConstructor(new Type[] { }); BlackBoxInt blackBox = (BlackBoxInt)ctorInt.Invoke(new object[] { }); FieldInfo field = classType.GetField("innerValue"); MethodInfo[] classNonPublicMethods = classType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); while (input != "END") { string[] commands = input.Split('_'); string command = commands[0]; int number = int.Parse(commands[1]); switch (command) { case "Add": MethodInfo info = classType.GetMethod("Add"); info.Invoke(blackBox, new object[] { number }); int fieldValue = (int)field.GetValue(blackBox); break; case "Subtract": break; case "Divide": break; case "Multiply": break; case "LeftShift": break; case "RightShift": break; default: break; } input = Console.ReadLine(); } }
static void Main(string[] args) { Type myType = typeof(BlackBoxInt); FieldInfo[] allFields = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); FieldInfo innerValue = allFields.First(x => x.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(innerValue.GetValue(testBlackBox)); input = Console.ReadLine(); } }
static void Main(string[] args) { Type myType = typeof(BlackBoxInt); ConstructorInfo ctor = myType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null); BlackBoxInt blackBoxIntInstance = (BlackBoxInt)ctor.Invoke(new object[] { }); //BlackBoxInt blackBoxIntInstance = (BlackBoxInt) Activator.CreateInstance(myType, true); FieldInfo field = myType.GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance); string input; while ((input = Console.ReadLine()) != "END") { string[] tolks = input.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries); MethodInfo method = myType.GetMethod(tolks[0], BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(blackBoxIntInstance, new object[] { int.Parse(tolks[1]) }); Console.WriteLine(field.GetValue(blackBoxIntInstance)); } }
public static void Main() { // Make instance BlackBoxInt blackBox = (BlackBoxInt)Activator.CreateInstance(typeof(BlackBoxInt), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[0], null); // Process while (true) { string line = Console.ReadLine(); if (line == "END") { break; } string[] cmdArgs = line.Split('_'); Console.WriteLine(GetInnerValue(blackBox, cmdArgs)); } }
public static void Main() { BlackBoxInt blackBox = (BlackBoxInt)Activator.CreateInstance(typeof(BlackBoxInt), true); string input; while ((input = Console.ReadLine()) != "END") { var command = input.Split('_')[0]; var number = int.Parse(input.Split('_')[1]); var method = typeof(BlackBoxInt).GetMethod(command, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); method.Invoke(blackBox, new object[] { number }); var value = typeof(BlackBoxInt).GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(blackBox); Console.WriteLine(value); } }
public static void Main() { Type classType = typeof(BlackBoxInt); // --public constructor-- //BlackBoxInt bbI = (BlackBoxInt)Activator.CreateInstance(classType, 0); // Option 1 (initialize object from a private constructor): Type[] paramTypes = new Type[] { typeof(int) }; var constructor = classType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, paramTypes, null); BlackBoxInt bbI = (BlackBoxInt)constructor.Invoke(new object[] { 0 }); //Option 2 (initialize object from a private constructor): //Type[] paramTypes = new Type[] { typeof(int) }; //object[] paramValues = new object[] { 0 }; //BlackBoxInt bbI = Construct<BlackBoxInt>(paramTypes, paramValues); string input; while ((input = Console.ReadLine()) != "END") { var inputParams = input.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries); int @value = int.Parse(inputParams[1]); MethodInfo method = classType .GetMethod(inputParams[0], BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(bbI, new object[] { @value }); FieldInfo currentField = classType .GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance); Console.WriteLine(currentField.GetValue(bbI)); } }
public static void Main() { Type blackBoxType = typeof(BlackBoxInt); BlackBoxInt blackBox = (BlackBoxInt)Activator.CreateInstance(blackBoxType, true); string input; while ((input = Console.ReadLine()) != "END") { string[] tokens = input.Split('_'); string method = tokens[0]; int value = int.Parse(tokens[1]); blackBoxType.GetMethod(method, BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(blackBox, new object[] { value }); string innerState = blackBoxType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First() .GetValue(blackBox) .ToString(); Console.WriteLine(innerState); } }
private static void Main() { Type type = typeof(BlackBoxInt); BlackBoxInt blackBox = (BlackBoxInt)Activator.CreateInstance(type, true); string line = Console.ReadLine(); while (line != "END") { string[] items = line.Split('_'); string method = items[0]; int value = int.Parse(items[1]); type.GetMethod(method, BindingFlags.NonPublic | BindingFlags.Instance) .Invoke(blackBox, new object[] { value }); object state = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).First() .GetValue(blackBox); Console.WriteLine(state); line = Console.ReadLine(); } }