Ejemplo n.º 1
0
        public string ToFileFormat()
        {
            var defaults = new ServerProperties();
            var sb       = new StringBuilder();

            foreach (var p in typeof(ServerProperties).GetProperties())
            {
                var info = (ServerPropertyAttribute)p.GetCustomAttributes(typeof(ServerPropertyAttribute), true).SingleOrDefault();
                if (info != null)
                {
                    var val = p.GetValue(this);
                    if (!val.Equals(p.GetValue(defaults)))
                    {
                        sb.Append(info.Name);
                        sb.Append("=");
                        if (p.PropertyType.IsEnum)
                        {
                            sb.AppendLine(((int)val).ToString());
                        }
                        else if (p.PropertyType == typeof(bool))
                        {
                            sb.AppendLine((bool)val ? "true" : "false");
                        }
                        else
                        {
                            sb.AppendLine(val.ToString());
                        }
                    }
                }
            }

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        public static ServerProperties FromFileFormat(string fileSettings)
        {
            var props = new ServerProperties();

            using (var tr = new StringReader(fileSettings)) {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    var i = line.IndexOf('#');
                    if (i >= 0)
                    {
                        line = line.Substring(0, i);
                    }
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    i = line.IndexOf('=');
                    if (i > 0 && i < line.Length - 1)
                    {
                        var propName = line.Substring(0, i);
                        var value    = line.Substring(i + 1);


                        var property = typeof(ServerProperties).GetProperties().SingleOrDefault(p => p.GetCustomAttributes <ServerPropertyAttribute>().SingleOrDefault()?.Name == propName);
                        if (property != null)
                        {
                            if (property.PropertyType.IsEnum)
                            {
                                property.SetValue(props, Enum.Parse(property.PropertyType, value));
                            }
                            else if (property.PropertyType == typeof(int))
                            {
                                if (int.TryParse(value, out var ival))
                                {
                                    property.SetValue(props, ival);
                                }
                                else
                                {
                                    throw new FileParseException($"Property {propName} should contain an integer");
                                }
                            }
                            else if (property.PropertyType == typeof(bool))
                            {
                                if (bool.TryParse(value, out var bval))
                                {
                                    property.SetValue(props, bval);
                                }
                                else
                                {
                                    throw new FileParseException($"Property {propName} should contain a boolean");
                                }
                            }
                            else
                            {
                                property.SetValue(props, value);
                            }
                        }
                        else
                        {
                            throw new FileParseException($"Unknown server property {propName}");
                        }
                    }
                }
            }

            return(props);
        }