コード例 #1
0
        protected T ParseNode <T>(XContainer node)
            where T : new()
        {
            var properties = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            T rtn = ObjectCreator <T> .Instance.Create(Type.EmptyTypes, null);

            foreach (var property in properties)
            {
                if (property.PropertyType.IsGenericType)
                {
                    XmlParseInfoAttribute parseInfo = XmlParseInfoAttribute.GetAttribute(property);
                    if (parseInfo == null)
                    {
                        throw new Exception(string.Format("{1}.{0}未定义XmlParseInfoAttribute", property.Name, property.DeclaringType.Name));
                    }
                    else
                    {
                        var value = ParseList(property.PropertyType, node, parseInfo.NodeName, parseInfo.ChildNodeName);
                        FastReflection.SetPropertyValue <object>(rtn, property, value);
                    }
                }
                else
                {
                    string valueStr = ReadValueStr(node, property.Name);
                    if (!string.IsNullOrEmpty(valueStr))
                    {
                        if (property.PropertyType == typeof(string))
                        {
                            FastReflection <T> .SetPropertyValue <string>(rtn, property, valueStr);
                        }
                        else if (property.PropertyType == typeof(Int32))
                        {
                            int value = DataHelper.TryParseInt32(valueStr);
                            FastReflection <T> .SetPropertyValue <Int32>(rtn, property, value);
                        }
                        else if (property.PropertyType == typeof(Int64))
                        {
                            Int64 value = DataHelper.TryParseInt64(valueStr);
                            FastReflection <T> .SetPropertyValue <Int64>(rtn, property, value);
                        }
                        else if (property.PropertyType == typeof(Double))
                        {
                            Double value = DataHelper.TryParseDouble(valueStr);
                            FastReflection <T> .SetPropertyValue <Double>(rtn, property, value);
                        }
                        else if (property.PropertyType == typeof(Decimal))
                        {
                            Decimal value = DataHelper.TryParseDecimal(valueStr);
                            FastReflection <T> .SetPropertyValue <Decimal>(rtn, property, value);
                        }
                        else if (property.PropertyType == typeof(Boolean))
                        {
                            Boolean value = DataHelper.TryParseBoolean(valueStr);
                            FastReflection <T> .SetPropertyValue <Boolean>(rtn, property, value);
                        }
                        else if (property.PropertyType.IsEnum)
                        {
                            int value = DataHelper.TryParseInt32(valueStr);

                            if (Enum.IsDefined(property.PropertyType, value))
                            {
                                FastReflection <T> .SetPropertyValue <object>(rtn, property, value);
                            }
                        }
                    }
                }
            }
            return(rtn);
        }
コード例 #2
0
        protected object ParseNode(Type returnType, XContainer node)
        {
            if (returnType.IsEnum)
            {
                string valueStr = (node as XElement).Value;
                int    value    = 0;
                if (!string.IsNullOrEmpty(valueStr))
                {
                    value = DataHelper.TryParseInt32(valueStr);
                }
                return(value);
            }
            else
            {
                var properties = returnType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

                object rtn = ObjectCreator.Create(returnType, Type.EmptyTypes, null);

                foreach (var property in properties)
                {
                    string valueStr = ReadValueStr(node, property.Name);
                    if (!string.IsNullOrEmpty(valueStr))
                    {
                        if (property.PropertyType == typeof(string))
                        {
                            FastReflection.SetPropertyValue <string>(rtn, property, valueStr);
                        }
                        else if (property.PropertyType == typeof(Int32))
                        {
                            int value = DataHelper.TryParseInt32(valueStr);
                            FastReflection.SetPropertyValue <Int32>(rtn, property, value);
                        }
                        else if (property.PropertyType == typeof(Int64))
                        {
                            Int64 value = DataHelper.TryParseInt64(valueStr);
                            FastReflection.SetPropertyValue <Int64>(rtn, property, value);
                        }
                        else if (property.PropertyType == typeof(Double))
                        {
                            Double value = DataHelper.TryParseDouble(valueStr);
                            FastReflection.SetPropertyValue <Double>(rtn, property, value);
                        }
                        else if (property.PropertyType == typeof(Decimal))
                        {
                            Decimal value = DataHelper.TryParseDecimal(valueStr);
                            FastReflection.SetPropertyValue <Decimal>(rtn, property, value);
                        }
                        else if (property.PropertyType == typeof(Boolean))
                        {
                            Boolean value = DataHelper.TryParseBoolean(valueStr);
                            FastReflection.SetPropertyValue <Boolean>(rtn, property, value);
                        }
                        else if (property.PropertyType.IsEnum)
                        {
                            int value = DataHelper.TryParseInt32(valueStr);

                            if (Enum.IsDefined(property.PropertyType, value))
                            {
                                FastReflection.SetPropertyValue <object>(rtn, property, value);
                            }
                        }
                    }
                    else if (property.PropertyType.IsGenericType)
                    {
                        XmlParseInfoAttribute parseInfo = XmlParseInfoAttribute.GetAttribute(property);
                        var value = ParseList(property.PropertyType, node, parseInfo.NodeName, parseInfo.ChildNodeName);
                        FastReflection.SetPropertyValue <object>(rtn, property, value);
                    }
                }
                return(rtn);
            }
        }