Esempio n. 1
0
        public override void Match(string line)
        {
            if (!MatchKeyValue(line, out var key, out var value))
            {
                throw new Exception("Unknown Key-Value: " + line);
            }

            var prop = _propertyInfos.FirstOrDefault(k => k.name == key).propInfo;

            if (prop == null)
            {
                if (UndefinedPairs == null)
                {
                    UndefinedPairs = new Dictionary <string, string>();
                }
                UndefinedPairs.Add(key, value);
            }
            else
            {
                var propType = prop.GetMethod.ReturnType;
                var attr     = prop.GetCustomAttribute <SectionConverterAttribute>();

                if (attr != null)
                {
                    var converter = attr.GetConverter();
                    prop.SetValue(this, converter.ReadSection(value, propType));
                }
                else if (propType.BaseType == typeof(Enum))
                {
                    prop.SetValue(this, Enum.Parse(propType, value));
                }
                else
                {
                    if (ValueConvert.ConvertValue(value, propType, out var converted))
                    {
                        prop.SetValue(this, converted);
                    }
                    else
                    {
                        throw new MissingMethodException($"Can not convert {{{value}}} to type {propType}.");
                    }
                }
            }
        }