Example #1
0
        //protected override string ResolvePropertyName(string propertyName)
        //{
        //	return base.ResolvePropertyName(propertyName);
        //}

        //protected override JsonProperty CreatePropertyFromConstructorParameter(JsonProperty matchingMemberProperty, ParameterInfo parameterInfo)
        //{
        //	return base.CreatePropertyFromConstructorParameter(matchingMemberProperty, parameterInfo);
        //}

        //protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
        //{
        //	return base.CreateMemberValueProvider(member);
        //}

        /// <summary>
        ///
        /// </summary>
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

            if (member.DeclaringType == typeof(Exception))
            {
                string valueName;
                if (ExceptionFieldValueToNameDictionary.TryGetValue(member.Name, out valueName))
                {
                    //property.ValueProvider = new ValueProvider(member.Name, valueName, member);
                    var valueProvider = new ValueProvider(PropertyReflector.MakeObjectPropertyGetter(PropertyReflector.GetProperty(typeof(Exception), member.Name)),
                                                          PropertyReflector.MakeObjectFieldSetter(PropertyReflector.GetField(typeof(Exception), valueName)))
                    {
                        Tag = string.Format("DeclaringType:{0}, Read: {1}, Write: {2}", member.DeclaringType.FullName, member.Name, valueName)
                    };
                    property.ValueProvider = valueProvider;
                    property.Writable      = true;
                }
            }

            return(property);
        }
Example #2
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;
                }
            }
        }