Ejemplo n.º 1
0
        internal static void PropertyReflectTest()
        {
            {
                var fGetter = PropertyReflector.MakeObjectFieldGetter(PropertyReflector.GetField(typeof(MyClass), "Name"));
                var fSetter = PropertyReflector.MakeObjectFieldSetter(PropertyReflector.GetField(typeof(MyClass), "Name"));

                var pGetter = PropertyReflector.MakeObjectPropertyGetter(PropertyReflector.GetProperty(typeof(MyClass), "Weight"));
                var pSetter = PropertyReflector.MakeObjectPropertySetter(PropertyReflector.GetProperty(typeof(MyClass), "Weight"));

                try
                {
                    var mc1 = new MyClass();

                    var name = "Chris";
                    fSetter(mc1, name);
                    var rname = fGetter(mc1);
                    if (name != (string)rname)
                    {
                        throw new Exception();
                    }

                    var weight = 62;
                    pSetter(mc1, weight);
                    var rweight = (int)pGetter(mc1);
                    if (weight != rweight)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("****** PropertyReflectTest() test failed");
                    throw;
                }
            }

            {
                var fGetter = PropertyReflector <MyClass, string> .GetFieldGetterFunc("Name");

                var fSetter = PropertyReflector <MyClass, string> .GetFieldSetterFunc("Name");

                var pGetter = PropertyReflector <MyClass, int> .GetPropertyGetterFunc("Weight");

                var pSetter = PropertyReflector <MyClass, int> .GetPropertySetterFunc("Weight");

                try
                {
                    var mc1 = new MyClass();

                    var name = "Chris";
                    fSetter(mc1, name);
                    var rname = fGetter(mc1);
                    if (name != rname)
                    {
                        throw new Exception();
                    }

                    var weight = 62;
                    pSetter(mc1, weight);
                    var rweight = pGetter(mc1);
                    if (weight != rweight)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("****** PropertyReflectTest() generic test failed");
                    throw;
                }
            }
        }