Пример #1
0
        private void SetPrimitiveProperty(IReflectionProperty property, object instance, XElement root)
        {
            XmlPropertyAttribute attribute = (XmlPropertyAttribute)property.Attributes.SingleOrDefault(x => x is XmlPropertyAttribute);

            var propertyName = property.Name;

            if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Name))
            {
                propertyName = attribute.Name;
            }

            var element = root.Element(propertyName);

            if (element != null)
            {
                var value = element.Value;

                if (!string.IsNullOrWhiteSpace(value))
                {
                    property.Set(instance, value);
                }
            }
        }
Пример #2
0
        private static void SetPropertyValue <T>(T config, IReadOnlyList <string> propertyAccessors, string value)
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                IReflectionType     reflectionType     = Reflector.Get(typeof(T));
                var                 jsonSerializer     = Container.Get <IJsonSerializer>();
                object              propertyValue      = reflectionType.GetPropertyValue(propertyAccessors[0], config);
                IReflectionProperty reflectionProperty = reflectionType.GetProperty(propertyAccessors[0]);

                if (reflectionProperty != null)
                {
                    if (reflectionProperty.IsPrimitive)
                    {
                        object deserializeValue = jsonSerializer.Deserialize(reflectionProperty.Type, value);

                        reflectionProperty.Set(config, deserializeValue);
                    }
                    else
                    {
                        reflectionType = Reflector.Get(reflectionProperty.Type);
                        for (int i = 1; i < propertyAccessors.Count - 1; i++)
                        {
                            propertyValue  = reflectionType.GetPropertyValue(propertyAccessors[i], propertyValue);
                            reflectionType = Reflector.Get(reflectionType.GetProperty(propertyAccessors[i]).Type);
                        }

                        IReflectionProperty property =
                            reflectionType.GetProperty(propertyAccessors[propertyAccessors.Count - 1]);

                        object deserializeValue = jsonSerializer.Deserialize(property.Type, value);

                        property.Set(propertyValue, deserializeValue);
                    }
                }
            }
        }