コード例 #1
0
        public StringMapTypeDeserializer(Type type)
        {
            this.type = type;

            if (type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable <>)))
            {
                return;
            }

            foreach (var propertyInfo in type.GetProperties())
            {
                var propertySetFn         = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo);
                var propertyType          = propertyInfo.PropertyType;
                var propertyParseStringFn = JsvReader.GetParseFn(propertyType);
                var propertySerializer    = new PropertySerializerEntry(propertySetFn, propertyParseStringFn)
                {
                    PropertyType = propertyType
                };

                var attr = propertyInfo.FirstAttribute <DataMemberAttribute>();
                if (attr != null && attr.Name != null)
                {
                    propertySetterMap[attr.Name] = propertySerializer;
                }
                propertySetterMap[propertyInfo.Name] = propertySerializer;
            }
        }
コード例 #2
0
        public void Get_setter_method_for_simple_properties()
        {
            Type                type         = typeof(Test);
            PropertyInfo        propertyInfo = type.GetProperty("TestProperty");
            SetPropertyDelegate setMethod    = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo);
            Test                test         = new Test();

            setMethod.Invoke(test, "test");
            Assert.AreEqual("test", test.TestProperty);
        }
コード例 #3
0
        public StringMapTypeDeserializer(Type type)
        {
            this.type = type;

            foreach (var propertyInfo in type.GetProperties())
            {
                var propertySetFn         = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo);
                var propertyParseStringFn = JsvReader.GetParseFn(propertyInfo.PropertyType);
                var propertySerializer    = new PropertySerializerEntry(propertySetFn, propertyParseStringFn);
                propertySetterMap.Add(propertyInfo.Name, propertySerializer);
            }
        }
コード例 #4
0
        public void Get_setter_method_for_dictionary_properties()
        {
            var  dict = new Dictionary <string, string>();
            Type type = typeof(Dictionary <string, string>);

            foreach (var propertyInfo in type.GetProperties())
            {
                SetPropertyDelegate setMethod = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo);
                if (setMethod == null)
                {
                    continue;
                }
                Console.WriteLine(propertyInfo.Name);
                setMethod.Invoke(dict, propertyInfo.Name);
            }
        }
コード例 #5
0
        public StringMapTypeDeserializer(Type type)
        {
            this.type = type;

            if (type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable <>)))
            {
                return;
            }

            foreach (var propertyInfo in type.GetProperties())
            {
                var propertySetFn         = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo);
                var propertyParseStringFn = JsvReader.GetParseFn(propertyInfo.PropertyType);
                var propertySerializer    = new PropertySerializerEntry(propertySetFn, propertyParseStringFn);
                propertySetterMap.Add(propertyInfo.Name, propertySerializer);
            }
        }
コード例 #6
0
        public StringMapTypeDeserializer(Type type)
        {
            this.type = type;

            foreach (var propertyInfo in type.GetSerializableProperties())
            {
                var propertySetFn         = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo);
                var propertyType          = propertyInfo.PropertyType;
                var propertyParseStringFn = GetParseFn(propertyType);
                var propertySerializer    = new PropertySerializerEntry(propertySetFn, propertyParseStringFn)
                {
                    PropertyType = propertyType
                };

                var attr = propertyInfo.FirstAttribute <DataMemberAttribute>();
                if (attr != null && attr.Name != null)
                {
                    propertySetterMap[attr.Name] = propertySerializer;
                }
                // Enhanced by William
                var attr2 = propertyInfo.FirstAttribute <XmlElementAttribute>();
                if (attr2 != null && attr2.ElementName != null)
                {
                    propertySetterMap[attr2.ElementName] = propertySerializer;
                }
                propertySetterMap[propertyInfo.Name] = propertySerializer;
            }

            if (JsConfig.IncludePublicFields)
            {
                foreach (var fieldInfo in type.GetSerializableFields())
                {
                    var fieldSetFn         = JsvDeserializeType.GetSetFieldMethod(type, fieldInfo);
                    var fieldType          = fieldInfo.FieldType;
                    var fieldParseStringFn = JsvReader.GetParseFn(fieldType);
                    var fieldSerializer    = new PropertySerializerEntry(fieldSetFn, fieldParseStringFn)
                    {
                        PropertyType = fieldType
                    };

                    propertySetterMap[fieldInfo.Name] = fieldSerializer;
                }
            }
        }