Exemplo n.º 1
0
        private static T NodeToModel <T>(XmlNode node) where T : new()
        {
            T val = new T();

            if (node.NodeType == XmlNodeType.Element)
            {
                XmlElement     xmlElement = (XmlElement)node;
                PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (XmlAttribute attribute in xmlElement.Attributes)
                {
                    string         b     = attribute.Name.ToLower();
                    string         text  = attribute.Value.ToString();
                    PropertyInfo[] array = properties;
                    foreach (PropertyInfo propertyInfo in array)
                    {
                        if (propertyInfo?.CanWrite ?? false)
                        {
                            string a = propertyInfo.Name.ToLower();
                            if (a == b && !string.IsNullOrEmpty(text))
                            {
                                Type propertyType = propertyInfo.PropertyType;
                                if (propertyType == typeof(string))
                                {
                                    propertyInfo.SetValue(val, text, null);
                                }
                                else if (propertyType == typeof(int))
                                {
                                    propertyInfo.SetValue(val, FPUtils.StrToInt(text), null);
                                }
                                else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
                                {
                                    propertyInfo.SetValue(val, FPUtils.StrToDateTime(text), null);
                                }
                                else if (propertyType == typeof(decimal))
                                {
                                    propertyInfo.SetValue(val, FPUtils.StrToDecimal(text), null);
                                }
                                else if (propertyType == typeof(float))
                                {
                                    propertyInfo.SetValue(val, FPUtils.StrToFloat(text), null);
                                }
                                else if (propertyType == typeof(double))
                                {
                                    propertyInfo.SetValue(val, FPUtils.StrToDouble(text), null);
                                }
                                else if (propertyType == typeof(bool))
                                {
                                    propertyInfo.SetValue(val, FPUtils.StrToBool(text, false), null);
                                }
                                else if (propertyType == typeof(short))
                                {
                                    propertyInfo.SetValue(val, short.Parse(FPUtils.StrToInt(text).ToString()), null);
                                }
                            }
                        }
                    }
                }
            }
            return(val);
        }
Exemplo n.º 2
0
        public static T ReadIni <T>(string filename) where T : new()
        {
            T val = new T();

            if (!File.Exists(filename))
            {
                WriteIni(val, filename);
                return(val);
            }
            Type typeFromHandle = typeof(T);

            PropertyInfo[] properties = typeFromHandle.GetProperties();
            foreach (PropertyInfo propertyInfo in properties)
            {
                if (propertyInfo != null && propertyInfo.CanWrite)
                {
                    string text = ReadIni(typeFromHandle.Name, propertyInfo.Name, filename);
                    if (propertyInfo.PropertyType == typeof(string))
                    {
                        propertyInfo.SetValue(val, text, null);
                    }
                    else if (propertyInfo.PropertyType == typeof(int))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToInt(text), null);
                    }
                    else if (propertyInfo.PropertyType == typeof(short))
                    {
                        propertyInfo.SetValue(val, short.Parse(FPUtils.StrToInt(text).ToString()), null);
                    }
                    else if (propertyInfo.PropertyType == typeof(DateTime))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToDateTime(text), null);
                    }
                    else if (propertyInfo.PropertyType == typeof(decimal))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToDecimal(text), null);
                    }
                    else if (propertyInfo.PropertyType == typeof(float))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToFloat(text), null);
                    }
                    else if (propertyInfo.PropertyType == typeof(double))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToDouble(text), null);
                    }
                    else if (propertyInfo.PropertyType == typeof(DateTime?) && propertyInfo.PropertyType != null)
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToDateTime(text), null);
                    }
                }
            }
            return(val);
        }
Exemplo n.º 3
0
        public static T LoadModel <T>(string filename) where T : new()
        {
            T           val            = new T();
            Type        typeFromHandle = typeof(T);
            XmlDocument xmlDocument    = new XmlDocument();

            try
            {
                xmlDocument.Load(filename);
            }
            catch
            {
                return(default(T));
            }
            PropertyInfo[] properties = typeFromHandle.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            PropertyInfo[] array      = properties;
            foreach (PropertyInfo propertyInfo in array)
            {
                if (!(propertyInfo?.CanWrite ?? false))
                {
                    continue;
                }
                XmlNode xmlNode = xmlDocument.SelectSingleNode(typeFromHandle.Name + "/" + propertyInfo.Name);
                if (xmlNode != null)
                {
                    Type propertyType = propertyInfo.PropertyType;
                    if (propertyType == typeof(string))
                    {
                        propertyInfo.SetValue(val, xmlNode.InnerText, null);
                    }
                    else if (propertyType == typeof(int))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToInt(xmlNode.InnerText), null);
                    }
                    else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToDateTime(xmlNode.InnerText), null);
                    }
                    else if (propertyType == typeof(decimal))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToDecimal(xmlNode.InnerText), null);
                    }
                    else if (propertyType == typeof(float))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToFloat(xmlNode.InnerText), null);
                    }
                    else if (propertyType == typeof(double))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToDouble(xmlNode.InnerText), null);
                    }
                    else if (propertyType == typeof(bool))
                    {
                        propertyInfo.SetValue(val, FPUtils.StrToBool(xmlNode.InnerText, false), null);
                    }
                    else if (propertyType == typeof(short))
                    {
                        propertyInfo.SetValue(val, short.Parse(FPUtils.StrToInt(xmlNode.InnerText).ToString()), null);
                    }
                }
            }
            return(val);
        }