public static void SaveProperties(object obj, XElement configEl) { foreach (var property in obj.GetType().GetProperties( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { var attr = property.GetCustomAttributes( typeof(ModulePropertyAttribute), true).OfType <ModulePropertyAttribute>().FirstOrDefault(); if (attr != null) { try { object val = property.GetValue(obj, null); if (val != null) { var converter = BotModule.GetTypeConverter(property); configEl.Add(new XAttribute(attr.ConfigName, converter.ConvertToString(val))); } } catch (Exception ex) { throw new BotConfigException(string.Format( "Could not save configuration property {0}", attr.ConfigName), ex); } } } }
public static void LoadProperties(object obj, XElement configEl) { foreach (var property in obj.GetType().GetProperties( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { var attr = property.GetCustomAttributes( typeof(ModulePropertyAttribute), true).OfType <ModulePropertyAttribute>().FirstOrDefault(); if (attr != null) { var configAt = configEl.Attribute(attr.ConfigName); if (configAt != null) { try { var converter = BotModule.GetTypeConverter(property); property.SetValue(obj, converter.ConvertFromString(configAt.Value), null); } catch (Exception ex) { throw new BotConfigException(string.Format( "Could not load configuration property {0}", attr.ConfigName), ex); } } } } }