コード例 #1
0
        private List <(BattleForgeSettingAttribute, string)> GetBattleForgeValues()
        {
            List <(BattleForgeSettingAttribute, string)> values = new List <(BattleForgeSettingAttribute, string)>();

            foreach (var setting in _settings)
            {
                BattleForgeSettingAttribute attr = setting.Item1;
                PropertyInfo prop = setting.Item2;

                object val = prop.GetValue(this);

                if (val != null)
                {
                    if (prop.PropertyType == typeof(bool))
                    {
                        values.Add((attr, ((bool)val) ? "1" : "0"));
                    }
                    else if (prop.PropertyType.IsPrimitive || prop.PropertyType == typeof(string))
                    {
                        values.Add((attr, val.ToString()));
                    }
                }
            }

            return(values);
        }
コード例 #2
0
        public void LoadValuesFromConfig(string xml)
        {
            foreach (var setting in _settings)
            {
                BattleForgeSettingAttribute attr = setting.Item1;
                PropertyInfo prop = setting.Item2;

                if (prop.SetMethod == null)
                {
                    continue;
                }

                string value = GetProperty(xml, attr.Category, attr.Property);

                if (value != null)
                {
                    if (prop.PropertyType == typeof(bool))
                    {
                        switch (value)
                        {
                        case "0":
                            prop.SetValue(this, false);
                            break;

                        case "1":
                            prop.SetValue(this, true);
                            break;

                        default:
                            prop.SetValue(this, Convert.ToBoolean(value));
                            break;
                        }
                    }
                    else if (prop.PropertyType == typeof(int))
                    {
                        prop.SetValue(this, Convert.ToInt32(value));
                    }
                    else if (prop.PropertyType == typeof(float))
                    {
                        prop.SetValue(this, Convert.ToSingle(value));
                    }
                    else if (prop.PropertyType == typeof(string))
                    {
                        prop.SetValue(this, value);
                    }
                }
            }
        }