Example #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);
        }
Example #2
0
        private static XElement BuildElement(IReflectionType reflectionType, string elementName = null)
        {
            XmlElementAttribute attribute = (XmlElementAttribute)reflectionType.Attributes.SingleOrDefault(x => x is XmlElementAttribute);

            XNamespace        defaultNamspace = null;
            List <XAttribute> namespaceList   = new List <XAttribute>();
            var nodeName = reflectionType.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);
        }
Example #3
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Deserializes.
        /// </summary>
        ///
        /// <exception cref="NotImplementedException">
        ///     Thrown when the requested operation is unimplemented.
        /// </exception>
        ///
        /// <param name="type">
        ///     The type.
        /// </param>
        /// <param name="value">
        ///     The value.
        /// </param>
        ///
        /// <returns>
        ///     .
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public object Deserialize(Type type, string value)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            IReflectionType reflectionType = Reflector.Get(type);

            object instance = reflectionType.CreateInstance();

            if (!string.IsNullOrWhiteSpace(value))
            {
                XDocument document = XDocument.Parse(value, LoadOptions.SetLineInfo);

                var root = document.Root;

                if (root != null)
                {
                    this.SetRootValue(reflectionType, instance, root);
                }
            }

            return(instance);
        }
Example #4
0
        public static Type GetReflectionType(ITypeId typeId)
        {
            IReflectionType reflectionType = typeId as IReflectionType;

            if (reflectionType != null)
            {
                return(reflectionType.ReflectionType);
            }
            return((Type)null);
        }
Example #5
0
        private void SetRootValue(IReflectionType reflectionType, object instance, XElement root)
        {
            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();

            SetPropertiesValue(properties, instance, root);
        }
Example #6
0
        private static Dictionary <string, IReflectionProperty> Build(Type type, string name = null)
        {
            Dictionary <string, IReflectionProperty> dictionary = new Dictionary <string, IReflectionProperty>();

            IReflectionType reflectionType = Reflector.Get(type);

            foreach (var property in reflectionType.Properties)
            {
                ParseProperty(dictionary, property, name);
            }

            return(dictionary);
        }
        public TemplateContext(
            TextWriter writer,
            object instance,
            bool disposeWriter = true,
            Func <string, string> templateLocator = null)
        {
            this.writer          = writer;
            this.disposeWriter   = disposeWriter;
            this.templateLocator = templateLocator;

            if (instance != null)
            {
                this.instance       = instance;
                this.reflectionType = Reflector.Get(instance);
            }
        }
Example #8
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);
        }
        protected virtual async Task <RestResponse <T> > ExecuteAsync <T>(RestRequest request, MethodType method) where T : new()
        {
            request.Method = method;
            RestResponse <T> response = new RestResponse <T>(await GetResponseAsync(request));

            try
            {
                if (!string.IsNullOrWhiteSpace(response.Content))
                {
                    switch (response.ResponseMode)
                    {
                    case ResponseMode.Json:
                        IJsonSerializer jsonSerializer = Container.Get <IJsonSerializer>();

                        response.ContentObject = jsonSerializer.Deserialize <T>(response.Content);
                        break;

                    case ResponseMode.Xml:
                        IXmlSerializer xmlSerializer = Container.Get <IXmlSerializer>();
                        response.ContentObject = xmlSerializer.Deserialize <T>(response.Content);
                        break;
                    }

                    if (typeof(ISerializable).IsAssignableFrom(typeof(T)))
                    {
                        if (response.ContentObject == null)
                        {
                            IReflectionType reflectionType = Reflector.Get <T>();
                            response.ContentObject = (T)reflectionType.CreateInstance();
                            ((ISerializable)response.ContentObject).Deserialize(response.Content);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                response.Status         = ResponseStatus.Error;
                response.ErrorMessage   = exception.Message;
                response.ErrorException = new RestException(exception.Message, response.StatusCode, response.ErrorException);
            }

            return(response);
        }
Example #10
0
        private void CheckBaseTypeInvariants()
        {
            string name = this.Name;

            if (!this.IsInterface)
            {
                if (this.baseType == null && this.type != null)
                {
                    Type baseType = this.type.BaseType;
                }
                IType type = this.baseType;
            }
            if (this.type != null && this.baseType != null)
            {
                IReflectionType reflectionType = this.baseType as IReflectionType;
                if (reflectionType == null)
                {
                    return;
                }
                Type reflectionType1 = reflectionType.ReflectionType;
            }
        }
Example #11
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);
            }
        }
Example #12
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);
                    }
                }
            }
        }