示例#1
0
    public static CarPropertiesSetting LoadSettingFromFile(string filename)
    {
        CarPropertiesSetting cps = new CarPropertiesSetting();

        PropertyInfo[] props = cps.GetType().GetProperties();

        if (File.Exists(filename))
        {
            string[] lines = File.ReadAllLines(filename);

            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].Split(':').Length == 2 && lines[i].Split('=').Length == 2)
                {
                    string propName = lines[i].Split(':')[0];
                    string datatype = lines[i].Split(':')[1].Split('=')[0];
                    string strValue = lines[i].Split('=')[1];

                    for (int j = 0; j < props.Length; j++)
                    {
                        if (props[j].Name == propName)
                        {
                            props[j].SetValue(cps, getValFromStr(datatype, strValue), null);
                            break;
                        }
                    }
                }
            }
        }

        return(cps);
    }
示例#2
0
    public static bool SavePropsToFile(CarPropertiesSetting setting, string filename)
    {
        List <string> lines = new List <string>();

        PropertyInfo[] props = setting.GetType().GetProperties();
        for (int i = 0; i < props.Length; i++)
        {
            if (props[i].CanRead && props[i].CanWrite)
            {
                lines.Add(props[i].Name + ":" + props[i].PropertyType.Name + "=" + props[i].GetValue(setting, null));
            }
        }

        File.WriteAllLines(filename, lines.ToArray());

        return(true);
    }