Exemplo n.º 1
0
        private static XObject BuildPrimitiveProperty(IReflectionProperty property, object value)
        {
            XmlPropertyAttribute attribute = (XmlPropertyAttribute)property.Attributes.SingleOrDefault(x => x is XmlPropertyAttribute);

            XNamespace        defaultNamspace = null;
            List <XAttribute> namespaceList   = new List <XAttribute>();
            var nodeName = property.Name;

            if (property.IsEnumerable && !property.IsDictionary && property.EnumerableType != null)
            {
                nodeName = property.EnumerableType.Name;
            }

            if (attribute != null)
            {
                defaultNamspace = attribute.DefaultNamespace;
                namespaceList.AddRange(attribute.NamespaceList.Select(x => new XAttribute(XNamespace.Xmlns + x.Key, x.Value)).ToList());

                if (!string.IsNullOrWhiteSpace(attribute.Name))
                {
                    nodeName = attribute.Name;
                }

                if (property.IsEnumerable && !property.IsDictionary && property.EnumerableType != null && !string.IsNullOrWhiteSpace(attribute.ItemName))
                {
                    nodeName = attribute.ItemName;
                }
            }

            XElement xElement = defaultNamspace != null
                ? new XElement(defaultNamspace + nodeName, namespaceList) : new XElement(nodeName, namespaceList);

            xElement.SetValue(FormatValue(property, value));
            return(xElement);
        }
Exemplo n.º 2
0
        private static XElement BuildElement(IReflectionProperty property, string elementName = null)
        {
            XmlPropertyAttribute attribute = (XmlPropertyAttribute)property.Attributes.SingleOrDefault(x => x is XmlPropertyAttribute);

            XNamespace        defaultNamspace = null;
            List <XAttribute> namespaceList   = new List <XAttribute>();
            var nodeName = property.Name;



            if (attribute != null)
            {
                defaultNamspace = attribute.DefaultNamespace;
                namespaceList.AddRange(attribute.NamespaceList.Select(x => new XAttribute(XNamespace.Xmlns + x.Key, x.Value)).ToList());

                if (!string.IsNullOrWhiteSpace(attribute.Name))
                {
                    nodeName = attribute.Name;
                }
            }

            if (!string.IsNullOrWhiteSpace(elementName))
            {
                nodeName = elementName;
            }

            XElement xElement = defaultNamspace != null
                ? new XElement(defaultNamspace + nodeName, namespaceList) : new XElement(nodeName, namespaceList);

            return(xElement);
        }
Exemplo n.º 3
0
        private void SetProperty(object instance, XElement root, IReflectionProperty property)
        {
            if (property.IsPrimitive || property.IsNullable)
            {
                if (instance != null)
                {
                    SetPrimitiveProperty(property, instance, root);
                }
            }

            if (property.IsClass)
            {
                var    type  = Reflector.Get(property.Type);
                object value = null;
                if (property.CanRead)
                {
                    value = property.Get(instance);
                }

                if (value == null)
                {
                    if (property.CanWrite)
                    {
                        value = type.CreateInstance();
                    }
                }

                if (value != null)
                {
                    var properties = type.Properties.OrderBy(
                        p =>
                    {
                        OrderAttribute priority = p.Attributes.SingleOrDefault(x => x is OrderAttribute) as OrderAttribute;

                        return(priority != null ? priority.Value : int.MaxValue);
                    }).ToList();

                    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)
                    {
                        this.SetPropertiesValue(properties, value, element);
                    }
                }
            }
        }
Exemplo n.º 4
0
        private static object FormatValue(IReflectionProperty property, object value)
        {
            if (value != null)
            {
                if (property.Type == typeof(DateTime))
                {
                    return(new W3CDateTime((DateTime)value));
                }
            }

            return(value);
        }
Exemplo n.º 5
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        /// Gets property value.
        /// </summary>
        /// <remarks>
        /// Anwar Javed, 09/20/2013 6:50 PM.
        /// </remarks>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="objInstance">
        /// (optional) the instance.
        /// </param>
        /// <returns>
        /// The property value.
        /// </returns>
        /// -------------------------------------------------------------------------------------------------
        public object GetPropertyValue(string name, object objInstance = null)
        {
            if (this.expandInstance != null)
            {
                object result = this.expandInstance[name];
                return(result);
            }

            IReflectionProperty property = this.GetProperty(name);

            return(property == null ? null : property.Get(objInstance));
        }
Exemplo n.º 6
0
        private static XElement BuildPropertyRoot(object instance, IReflectionProperty rootProperty)
        {
            IReflectionType reflectionType = Reflector.Get(instance.GetType());
            XElement        root           = BuildElement(rootProperty);
            var             properties     = reflectionType.Properties.OrderBy(
                p =>
            {
                OrderAttribute priority = p.Attributes.SingleOrDefault(x => x is OrderAttribute) as OrderAttribute;

                return(priority != null ? priority.Value : int.MaxValue);
            }).ToList();

            foreach (var property in properties)
            {
                AddProperty(instance, root, property);
            }

            return(root);
        }
Exemplo n.º 7
0
        private static void AddProperty(object instance, XElement root, IReflectionProperty property)
        {
            object value = property.Get(instance);

            if (property.IsPrimitive || property.IsNullable)
            {
                if (value != null)
                {
                    if (property.Type == typeof(string))
                    {
                        if (!string.IsNullOrWhiteSpace(Convert.ToString(value)))
                        {
                            root.Add(BuildPrimitiveProperty(property, value));
                        }
                    }
                    else if (property.Type == typeof(int) || property.Type == typeof(long) || property.Type == typeof(short) ||
                             property.Type == typeof(float) || property.Type == typeof(double))
                    {
                        if (Convert.ToDouble(value) != 0)
                        {
                            root.Add(BuildPrimitiveProperty(property, value));
                        }
                    }

                    else
                    {
                        root.Add(BuildPrimitiveProperty(property, value));
                    }
                }
            }

            if (property.IsEnumerable)
            {
                root.Add(property.IsDictionary ? BuildDictionaryProperty(property, value)
                    : BuildEnumerableProperty(property, value));
            }

            if (property.IsClass)
            {
                root.Add(BuildPropertyRoot(value, property));
            }
        }
Exemplo n.º 8
0
        private static XObject BuildEnumerableProperty(IReflectionProperty property, object value)
        {
            XElement root = BuildElement(property);

            if (value != null)
            {
                if (property.EnumerableType.IsPrimitiveType())
                {
                    IEnumerable items = (IEnumerable)value;


                    foreach (var item in items)
                    {
                        root.Add(BuildPrimitiveProperty(property, item));
                    }
                }
            }

            return(root);
        }
Exemplo n.º 9
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);
                }
            }
        }
Exemplo n.º 10
0
        private static XObject BuildDictionaryProperty(IReflectionProperty property, object value)
        {
            XElement root = BuildElement(property);

            if (value != null)
            {
                if (property.KeyType.IsPrimitiveType() && property.EnumerableType.IsPrimitiveType())
                {
                    IDictionary items = (IDictionary)value;

                    foreach (DictionaryEntry item in items)
                    {
                        XmlPropertyAttribute attribute = (XmlPropertyAttribute)property.Attributes.SingleOrDefault(x => x is XmlPropertyAttribute);

                        XElement child = BuildElement(property, attribute != null ? attribute.ItemName : null);
                        //child.Add(Bu(property, attribute != null ? attribute.ItemName : null););
                        root.Add(child);
                    }
                }
            }

            return(root);
        }
Exemplo n.º 11
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);
                    }
                }
            }
        }
Exemplo n.º 12
0
        private static void ParseProperty(Dictionary <string, IReflectionProperty> dictionary, IReflectionProperty property, string parentName = "")
        {
            string propertyName = property.Name;

            if (!string.IsNullOrWhiteSpace(parentName))
            {
                propertyName = parentName + "." + propertyName;
            }

            if (property.IsClass)
            {
                IReflectionType reflectionType = Reflector.Get(property.Type);

                foreach (var reflectionProperty in reflectionType.Properties)
                {
                    ParseProperty(dictionary, reflectionProperty, propertyName);
                }
            }
            else
            {
                dictionary.Add(propertyName, property);
            }
        }
Exemplo n.º 13
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        /// Gets a property.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The property.
        /// </returns>
        /// -------------------------------------------------------------------------------------------------
        public IReflectionProperty GetProperty(string name)
        {
            IReflectionProperty property = this.Properties.FirstOrDefault(p => p.Name == name);

            return(property);
        }