Ejemplo n.º 1
0
        public static void Run()
        {
            Console.WriteLine(nameof(TestInstanceFields));

            TypeInfo ti = typeof(FieldInvokeSample).GetTypeInfo();

            FieldInfo         instanceField = ti.GetDeclaredField("InstanceField");
            FieldInvokeSample obj           = new FieldInvokeSample();

            String value = (String)(instanceField.GetValue(obj));

            if (value != null)
            {
                throw new Exception();
            }

            obj.InstanceField = "Hi!";
            value             = (String)(instanceField.GetValue(obj));
            if (value != "Hi!")
            {
                throw new Exception();
            }

            instanceField.SetValue(obj, "Bye!");
            if (obj.InstanceField != "Bye!")
            {
                throw new Exception();
            }

            value = (String)(instanceField.GetValue(obj));
            if (value != "Bye!")
            {
                throw new Exception();
            }
        }
Ejemplo n.º 2
0
    private static int TestReflectionFieldAccess()
    {
        Console.WriteLine("Testing reflection field access");

        if (string.Empty.Length > 0)
        {
            new FieldInvokeSample().ToString();
        }

        TypeInfo ti = typeof(FieldInvokeSample).GetTypeInfo();
        {
            FieldInfo         instanceField = ti.GetDeclaredField("InstanceField");
            FieldInvokeSample obj           = new FieldInvokeSample();

            String value = (String)(instanceField.GetValue(obj));
            if (value != null)
            {
                return(Fail);
            }

            obj.InstanceField = "Hi!";
            value             = (String)(instanceField.GetValue(obj));
            if (value != "Hi!")
            {
                return(Fail);
            }

            instanceField.SetValue(obj, "Bye!");
            if (obj.InstanceField != "Bye!")
            {
                return(Fail);
            }

            value = (String)(instanceField.GetValue(obj));
            if (value != "Bye!")
            {
                return(Fail);
            }

            return(Pass);
        }
    }