コード例 #1
0
        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       innerField  = type.GetField("innerValue", flags);

            string[] inputArgs = Console.ReadLine()
                                 .Split("_")
                                 .ToArray();

            while (inputArgs[0]?.ToLower() != "end")
            {
                string     methodName = inputArgs[0];
                int        number     = int.Parse(inputArgs[1]);
                MethodInfo method     = type.GetMethod(methodName, flags);
                method.Invoke(instance, new object[] { number });
                Console.WriteLine(innerField.GetValue(instance));

                inputArgs = Console.ReadLine()
                            .Split("_")
                            .ToArray();
            }
        }
コード例 #2
0
        public static void Main()
        {
            Type            blackBoxClass = typeof(BlackBoxInteger);
            BlackBoxInteger box           = (BlackBoxInteger)Activator.CreateInstance(blackBoxClass, true);

            while (true)
            {
                string input = Console.ReadLine();
                if (input == "END")
                {
                    break;
                }
                else
                {
                    string command = input.Split('_')[0];
                    int    number  = int.Parse(input.Split('_')[1]);
                    blackBoxClass.GetMethod(command, BindingFlags.Instance |
                                            BindingFlags.NonPublic)
                    .Invoke(box, new object[] { number });
                    int result = (int)blackBoxClass.GetField("innerValue", BindingFlags.Instance |
                                                             BindingFlags.NonPublic).GetValue(box);
                    Console.WriteLine(result);
                }
            }
            Console.ReadLine();
        }
コード例 #3
0
        public static void Main()
        {
            var type = typeof(BlackBoxInteger);
            //ConstructorInfo constructor = type.GetConstructor(new Type[] { typeof(int) });
            BlackBoxInteger instance = (BlackBoxInteger)Activator.CreateInstance(type, true);


            string input = Console.ReadLine();

            while (input != "END")
            {
                string[] tokens  = input.Split("_");
                string   command = tokens[0];
                int      data    = int.Parse(tokens[1]);

                var method = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(m => m.Name == command);
                method.Invoke(instance, new object[] { data });
                var fieldInfo = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).First(f => f.Name == "innerValue").GetValue(instance);

                Console.WriteLine(fieldInfo);



                input = Console.ReadLine();
            }
        }
コード例 #4
0
        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));
            }
        }
コード例 #5
0
        public static void Main()
        {
            Type blackBoxType = typeof(BlackBoxInteger);

            ConstructorInfo[] constructors = blackBoxType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
            BlackBoxInteger   blackBox     = (BlackBoxInteger)constructors[1].Invoke(new object[] { });

            FieldInfo innerVal = blackBoxType.GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance);

            MethodInfo[] methods = blackBoxType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);

            while (true)
            {
                string[] inputArgs = Console.ReadLine().Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);

                string command = inputArgs[0];

                if (command == "END")
                {
                    break;
                }

                int numberToOperateWith = int.Parse(inputArgs[1]);

                methods.Where(x => x.Name == command)
                .FirstOrDefault()
                .Invoke(blackBox, new object[] { numberToOperateWith });

                Console.WriteLine(innerVal.GetValue(blackBox));
            }
        }
コード例 #6
0
        public static void Main()
        {
            Type classInfo = typeof(BlackBoxInteger);

            BlackBoxInteger instance = (BlackBoxInteger)Activator.CreateInstance(classInfo, true);

            MethodInfo[] methods = classInfo.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);

            string input = Console.ReadLine();

            while (input != "END")
            {
                string[] commandLine = input.Split("_");
                string   command     = commandLine[0];
                int      value       = int.Parse(commandLine[1]);

                MethodInfo method = methods.Where(x => x.Name == command).First();

                method.Invoke(instance, new object [] { value });

                var innerValue = classInfo.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.Name == "innerValue").First();

                Console.WriteLine(innerValue.GetValue(instance));

                input = Console.ReadLine();
            }
        }
コード例 #7
0
        public static void Main()
        {
            Type typeOfBlackBoxInteger = typeof(BlackBoxInteger);

            ConstructorInfo blackBoxIntegerConstructor = typeOfBlackBoxInteger.GetConstructor
                                                             (BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(int) }, null);

            BlackBoxInteger blackBoxInteger = (BlackBoxInteger)blackBoxIntegerConstructor.Invoke(new object[] { 0 });

            string input;

            while ((input = Console.ReadLine()) != "END")
            {
                string[] commandSplit = input.Split('_');

                string methodName      = commandSplit[0];
                int    methodParameter = int.Parse(commandSplit[1]);

                typeOfBlackBoxInteger.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic)
                .Invoke(blackBoxInteger, new object[] { methodParameter });

                Console.WriteLine(typeOfBlackBoxInteger.GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic)
                                  .GetValue(blackBoxInteger));
            }

            //Console.WriteLine(typeOfBlackBoxInteger.GetField("innerValue",BindingFlags.Instance | BindingFlags.NonPublic)
            //    .GetValue(blackBoxInteger));
        }
コード例 #8
0
 private static void ExecuteCommand(Type type, BlackBoxInteger blackBoxInteger, string command, int number)
 {
     if (command == "Add")
     {
         InvokeMethod(type, blackBoxInteger, number, "Add");
     }
     else if (command == "Subtract")
     {
         InvokeMethod(type, blackBoxInteger, number, "Subtract");
     }
     else if (command == "Multiply")
     {
         InvokeMethod(type, blackBoxInteger, number, "Multiply");
     }
     else if (command == "Divide")
     {
         InvokeMethod(type, blackBoxInteger, number, "Divide");
     }
     else if (command == "LeftShift")
     {
         InvokeMethod(type, blackBoxInteger, number, "LeftShift");
     }
     else if (command == "RightShift")
     {
         InvokeMethod(type, blackBoxInteger, number, "RightShift");
     }
 }
コード例 #9
0
        private static void PrintResult(Type classType, BlackBoxInteger classInstance)
        {
            FieldInfo field = classType.GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance);

            field.GetValue(classInstance);

            Console.WriteLine(field.GetValue(classInstance));
        }
コード例 #10
0
        private static void InvokeMethod(Type type, BlackBoxInteger blackBoxInteger, int number, string methodName)
        {
            MethodInfo method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);

            method.Invoke(blackBoxInteger, new object[] { number });
            FieldInfo field = type.GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic);

            Console.WriteLine(field.GetValue(blackBoxInteger));
        }
コード例 #11
0
        public static void Print(MethodInfo[] methods, BlackBoxInteger instance, Type type, int value, string methodName)
        {
            var AddMethod = methods.FirstOrDefault(m => m.Name == methodName);

            AddMethod.Invoke(instance, new object[] { value });

            var field = type.GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instance);

            Console.WriteLine(field);
        }
コード例 #12
0
        private static void ProcessCommand(BlackBoxInteger blackBox, string methodName, int value)
        {
            MethodInfo method = typeof(BlackBoxInteger).GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);

            method.Invoke(blackBox, new object[] { value });

            FieldInfo field  = typeof(BlackBoxInteger).GetField("innerValue", BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
            int       result = (int)field.GetValue(blackBox);

            Console.WriteLine(result);
        }
コード例 #13
0
        private static void InvokeMethodAndPrintValue(Type blackBoxType, BlackBoxInteger blackBox, string methodName, int methodArg)
        {
            MethodInfo method = blackBoxType.GetMethod(methodName,
                                                       BindingFlags.Instance | BindingFlags.NonPublic);

            method.Invoke(blackBox, new object[] { methodArg });

            FieldInfo innerValue = blackBoxType.GetField("innerValue",
                                                         BindingFlags.Instance |
                                                         BindingFlags.NonPublic);

            Console.WriteLine(innerValue.GetValue(blackBox));
        }
コード例 #14
0
        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));
        }
コード例 #15
0
        public static void Main()
        {
            Type            type            = Type.GetType("P02_BlackBoxInteger.BlackBoxInteger");
            BlackBoxInteger blackBoxInteger = (BlackBoxInteger)Activator.CreateInstance(type, true);

            string[] commandParams;

            while ((commandParams = Console.ReadLine().Split('_'))[0] != "END")
            {
                string command = commandParams[0];
                int    number  = int.Parse(commandParams[1]);

                ExecuteCommand(type, blackBoxInteger, command, number);
            }
        }
コード例 #16
0
        public static void Main()
        {
            BlackBoxInteger blackBox = (BlackBoxInteger)Activator.CreateInstance(typeof(BlackBoxInteger), true);

            var input = Console.ReadLine();

            while (!input.Equals("END"))
            {
                var args       = input.Split('_');
                var methodName = args[0];
                var value      = int.Parse(args[1]);

                ProcessCommand(blackBox, methodName, value);

                input = Console.ReadLine();
            }
        }
コード例 #17
0
        static void ExecuteCommand(BlackBoxInteger instance)
        {
            string input;

            while ((input = Console.ReadLine()) != "END")
            {
                string[]   commandData   = input.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
                string     commandType   = commandData[0];
                int        value         = int.Parse(commandData[1]);
                MethodInfo currentMethod = null;

                switch (commandType)
                {
                case "Add":
                    currentMethod = typeof(BlackBoxInteger).GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance);
                    break;

                case "Subtract":
                    currentMethod = typeof(BlackBoxInteger).GetMethod("Subtract", BindingFlags.NonPublic | BindingFlags.Instance);
                    break;

                case "Multiply":
                    currentMethod = typeof(BlackBoxInteger).GetMethod("Multiply", BindingFlags.NonPublic | BindingFlags.Instance);
                    break;

                case "Divide":
                    currentMethod = typeof(BlackBoxInteger).GetMethod("Divide", BindingFlags.NonPublic | BindingFlags.Instance);
                    break;

                case "RightShift":
                    currentMethod = typeof(BlackBoxInteger).GetMethod("RightShift", BindingFlags.NonPublic | BindingFlags.Instance);
                    break;

                case "LeftShift":
                    currentMethod = typeof(BlackBoxInteger).GetMethod("LeftShift", BindingFlags.NonPublic | BindingFlags.Instance);
                    break;
                }

                currentMethod.Invoke(instance, new object[] { value });
                Console.WriteLine(typeof(BlackBoxInteger).GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(instance));
            }
        }
コード例 #18
0
        public static void Main()
        {
            Type            type        = typeof(BlackBoxInteger);
            var             constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(int) }, null);
            BlackBoxInteger instance    = (BlackBoxInteger)constructor.Invoke(new object[] { 0 });

            FieldInfo fieldInfo = type.GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance);
            string    input;

            while ((input = Console.ReadLine()) != "END")
            {
                string[] tokens = input.Split('_', StringSplitOptions.RemoveEmptyEntries);
                string   name   = tokens[0];
                int      value  = int.Parse(tokens[1]);

                MethodInfo method = type.GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance);
                method.Invoke(instance, new object[] { value });
                Console.WriteLine(fieldInfo.GetValue(instance));
            }
        }
コード例 #19
0
        public static void Main()
        {
            Type blackBoxType = typeof(BlackBoxInteger);

            ConstructorInfo constructor =
                blackBoxType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];

            BlackBoxInteger blackBox = (BlackBoxInteger)constructor.Invoke(new object[] { 0 });

            string input;

            while ((input = Console.ReadLine()) != "END")
            {
                string[] tokens     = input.Split("_");
                string   methodName = tokens[0];
                int      methodArg  = int.Parse(tokens[1]);

                InvokeMethodAndPrintValue(blackBoxType, blackBox, methodName, methodArg);
            }
        }
コード例 #20
0
        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));
            }
        }
コード例 #21
0
        public static void Main()
        {
            Type            blackBoxIntegerType = typeof(BlackBoxInteger);
            var             blackBoxConstructor = blackBoxIntegerType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[0], null);
            BlackBoxInteger blackBoxInteger     = (BlackBoxInteger)blackBoxConstructor.Invoke(new object[] { });

            var blackBoxMethods         = blackBoxIntegerType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
            var blackBoxFields          = blackBoxIntegerType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
            var blackBoxInnerValueField = blackBoxFields.FirstOrDefault(f => f.Name == "innerValue");

            string input;

            while ((input = Console.ReadLine()) != "END")
            {
                var inputData = input.Split("_");
                var command   = inputData[0];
                var value     = int.Parse(inputData[1]);

                InvokeMethod(command, value, blackBoxInteger, blackBoxInnerValueField, blackBoxMethods);
            }
        }
コード例 #22
0
        public static void Main()
        {
            Type            classType = typeof(BlackBoxInteger);
            BlackBoxInteger instance  = (BlackBoxInteger)Activator.CreateInstance(classType, true);
            string          command;

            while ((command = Console.ReadLine()) != "END")
            {
                var    tokens     = command.Split('_');
                string methodName = tokens[0];
                int    value      = int.Parse(tokens[1]);

                classType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                .Invoke(instance, new object[] { value });

                int currentValue = (int)classType.GetField("innerValue", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                   .GetValue(instance);

                Console.WriteLine(currentValue);
            }
        }
コード例 #23
0
        public static void Main()
        {
            string          methodTokens  = Console.ReadLine();
            Type            type          = typeof(BlackBoxInteger);
            var             constructor   = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(int) }, null);
            BlackBoxInteger classInstance = (BlackBoxInteger)constructor.Invoke(new object[] { 0 });

            while (methodTokens != "END")
            {
                string[] tokens = methodTokens.Split("_");
                string   name   = tokens[0];
                int      value  = int.Parse(tokens[1]);

                MethodInfo currentMethod = type.GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic);
                currentMethod.Invoke(classInstance, new object[] { value });
                FieldInfo field = type.GetField("innerValue", BindingFlags.NonPublic | BindingFlags.Instance);

                Console.WriteLine($"{field.GetValue(classInstance)}");

                methodTokens = Console.ReadLine();
            }
        }
コード例 #24
0

        
コード例 #25
0
        public static void Main()
        {
            Type classType = Assembly
                             .GetCallingAssembly()
                             .GetTypes()
                             .FirstOrDefault(x => x.Name is nameof(BlackBoxInteger));

            BlackBoxInteger instance = (BlackBoxInteger)Activator
                                       .CreateInstance(classType, true);

            string inputLine = Console.ReadLine();

            while (inputLine != "END")
            {
                string[] inputArguments = inputLine
                                          .Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);

                string methodName = inputArguments[0];
                int    number     = int.Parse(inputArguments[1]);

                classType.GetMethod(methodName,
                                    BindingFlags.Instance |
                                    BindingFlags.NonPublic |
                                    BindingFlags.Public)
                .Invoke(instance, new object[] { number });

                int currentResult = (int)classType.GetField("innerValue",
                                                            BindingFlags.Instance |
                                                            BindingFlags.NonPublic |
                                                            BindingFlags.Public)
                                    .GetValue(instance);

                Console.WriteLine(currentResult);

                inputLine = Console.ReadLine();
            }
        }
コード例 #26
0
        public static void Main()
        {
            Type type = typeof(BlackBoxInteger);

            ConstructorInfo ctor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);

            BlackBoxInteger classInstance = (BlackBoxInteger)ctor.Invoke(null);

            MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);

            string input;

            while ((input = Console.ReadLine()) != "END")
            {
                var    args       = input.Split(new[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
                string methodName = args[0];
                int    param      = int.Parse(args[1]);

                MethodInfo targetMethod = methods.FirstOrDefault(m => m.Name == methodName);
                FieldInfo  targetField  = type.GetField("innerValue");

                int index = 0;

                for (int i = 0; i < fields.Length; i++)
                {
                    if (fields[i] == targetField)
                    {
                        index = i;
                    }
                }
                targetMethod.Invoke(classInstance, new object[] { param });
                Console.WriteLine(fields[index].GetValue(classInstance));
            }
        }
コード例 #27
0
        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);
            }
        }
コード例 #28
0

        
コード例 #29
0
        public static void Main()
        {
            Type classType = typeof(BlackBoxInteger);

            MethodInfo[] methods = classType.GetMethods(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);

            BlackBoxInteger classInstance = (BlackBoxInteger)Activator.CreateInstance(typeof(BlackBoxInteger), true);

            var command = Console.ReadLine();

            while (command != "END")
            {
                string[] commandParts = command.Split("_");
                string   methodName   = commandParts[0];
                int      number       = int.Parse(commandParts[1]);

                methods.Where(x => x.Name == methodName).FirstOrDefault()?.Invoke(classInstance, new object[1] {
                    number
                });

                PrintResult(classType, classInstance);
                command = Console.ReadLine();
            }
        }
コード例 #30
0
        public static void Main()
        {
            Type classType = typeof(BlackBoxInteger);

            BlackBoxInteger blackBox = (BlackBoxInteger)Activator.CreateInstance(classType, true);

            string command = Console.ReadLine();

            while (!command.Equals("END"))
            {
                string[] inputCommand = command.Split('_');
                string   methodName   = inputCommand[0];
                int      passedValue  = int.Parse(inputCommand[1]);

                MethodInfo currentMethod = classType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);

                currentMethod.Invoke(blackBox, new object[] { passedValue });

                string innerValue = classType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).First().GetValue(blackBox).ToString();
                Console.WriteLine(innerValue);

                command = Console.ReadLine();
            }
        }