Example #1
0
        private static void SetItemProperties <T>(JsonParser parser, T item) where T : class
        {
            var type = typeof(T);

            if (!Setters.TryGetValue(type, out var setters))
            {
                setters = CreateSetters <T>();

                Setters[type] = setters;
            }

            if (!setters.Any())
            {
                return;
            }

            foreach (var propertyName in parser.JsonObject.Keys)
            {
                if (!setters.TryGetValue(propertyName, out var setter))
                {
                    continue;
                }

                var value = parser.JsonObject[propertyName];

                if (value == null)
                {
                    var existingValue = setter.PropertyInfo.GetValue(item);

                    if (existingValue != null)
                    {
                        throw new ArgumentException($"Can't reset {type.Name}.{propertyName} to null from existing value '{existingValue}'");
                    }

                    continue;
                }

                var propertyValue = setter.PropertyParser.Invoke(parser, new object[] { propertyName });

                setter.PropertyInfo.SetValue(item, propertyValue);
            }
        }
Example #2
0
 public static ISetter <T> GetSetter(string name)
 {
     return(Setters.TryGetValue(name));
 }