Exemplo n.º 1
0
        private static void InstallCustomConfig(IToolForm tool, Dictionary <string, object> data)
        {
            Type type  = tool.GetType();
            var  props = type.GetPropertiesWithAttrib(typeof(ConfigPersistAttribute)).ToList();

            if (props.Count == 0)
            {
                return;
            }

            foreach (var prop in props)
            {
                if (data.TryGetValue(prop.Name, out var val))
                {
                    if (val is string str && prop.PropertyType != typeof(string))
                    {
                        // if a type has a TypeConverter, and that converter can convert to string,
                        // that will be used in place of object markup by JSON.NET

                        // but that doesn't work with $type metadata, and JSON.NET fails to fall
                        // back on regular object serialization when needed.  so try to undo a TypeConverter
                        // operation here
                        var converter = TypeDescriptor.GetConverter(prop.PropertyType);
                        val = converter.ConvertFromString(null, CultureInfo.InvariantCulture, str);
                    }
                    else if (!(val is bool) && prop.PropertyType.IsPrimitive)
                    {
                        // numeric constants are similarly hosed
                        val = Convert.ChangeType(val, prop.PropertyType, CultureInfo.InvariantCulture);
                    }

                    prop.SetValue(tool, val, null);
                }
            }
Exemplo n.º 2
0
 private static bool HasCustomConfig(IToolForm tool)
 {
     return(tool.GetType().GetPropertiesWithAttrib(typeof(ConfigPersistAttribute)).Any());
 }