Esempio 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);
        }
Esempio n. 2
0
 public static int[] SplitInt(string strContent, string[] separator)
 {
     string[] array  = SplitString(strContent, separator);
     int[]    array2 = new int[array.Length];
     for (int i = 0; i < array.Length; i++)
     {
         array2[i] = FPUtils.StrToInt(array[i]);
     }
     return(array2);
 }
Esempio n. 3
0
 public static int InArray(int item, string[] stringArray)
 {
     for (int i = 0; i < stringArray.Length; i++)
     {
         if (FPUtils.StrToInt(stringArray[i]) == item)
         {
             return(i);
         }
     }
     return(-1);
 }
Esempio n. 4
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);
        }
Esempio n. 5
0
 public static int[] SplitInt(string strContent, string[] separator, int length)
 {
     int[]    array  = new int[length];
     string[] array2 = SplitString(strContent, separator);
     for (int i = 0; i < length; i++)
     {
         if (i < array2.Length)
         {
             array[i] = FPUtils.StrToInt(array2[i]);
         }
         else
         {
             array[i] = 0;
         }
     }
     return(array);
 }
Esempio n. 6
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);
        }