예제 #1
0
        private static string GetPropertyValue <T>(T config, IReadOnlyList <string> propertyAccessors)
        {
            IReflectionType reflectionType = Reflector.Get(typeof(T));
            var             jsonSerializer = Container.Get <IJsonSerializer>();
            object          propertyValue  = reflectionType.GetPropertyValue(propertyAccessors[0], config);

            reflectionType = Reflector.Get(reflectionType.GetProperty(propertyAccessors[0]).Type);

            for (int i = 1; i < propertyAccessors.Count; i++)
            {
                propertyValue  = reflectionType.GetPropertyValue(propertyAccessors[i], propertyValue);
                reflectionType = Reflector.Get(reflectionType.GetProperty(propertyAccessors[i]).Type);

                if (i == (propertyAccessors.Count - 2))
                {
                    i++;
                    propertyValue = reflectionType.GetPropertyValue(propertyAccessors[i], propertyValue);
                }
            }

            if (propertyValue != null)
            {
                string serializedValue = jsonSerializer.Serialize(propertyValue);

                return(serializedValue);
            }

            return(null);
        }
예제 #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);
                    }
                }
            }
        }