예제 #1
0
    public static void Main()
    {
        Type            classType = typeof(BlackBoxInteger);
        BlackBoxInteger instance  = (BlackBoxInteger)Activator.CreateInstance(classType, true);
        string          command;

        while ((command = Console.ReadLine()) != "END")
        {
            string[] 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);
        }
    }
예제 #2
0
    public void Run()
    {
        Type      classType = typeof(BlackBoxInteger);
        FieldInfo field     = classType.GetField("innerValue", BindingFlags.Instance | BindingFlags.NonPublic);

        MethodInfo[]    methods  = classType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
        BlackBoxInteger instance = (BlackBoxInteger)Activator.CreateInstance(classType, true);

        string input = this.reader.ReadLine();

        while (input != "END")
        {
            string[] parts      = input.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
            string   methodName = parts[0];
            int      number     = int.Parse(parts[1]);

            MethodInfo method = methods.First(m => m.Name == methodName);
            method.Invoke(instance, new object[] { number });
            this.writer.WriteLine(field.GetValue(instance).ToString());

            input = this.reader.ReadLine();
        }
    }