public void SetConfig <T>(string key, T value) { var prop = (from property in GetType().GetProperties() where property.Name == key || ((property.GetCustomAttributes(false).OfType <YamlMemberAttribute>().FirstOrDefault())?.Alias == key) select property).FirstOrDefault(); // I like to do it this way sometimes. Don't judge. prop.SetValue(this, value); ConfigSet?.Invoke(this, key); }
/// <summary> /// Sets a given config. /// </summary> /// <typeparam name="T">The config's type-</typeparam> /// <param name="key">The config's key.</param> /// <param name="value">The new value of the config.</param> public void SetConfig <T>(string key, T value) { if (HasKey(key)) { cfgHashMap.TryGetValue(key, out var curValue); if (!(curValue is T)) { throw new InvalidDataException($"Cannot convert { typeof(T).Name } to { curValue.GetType().Name }!"); } cfgHashMap.Remove(key); } cfgHashMap.Add(key, value); ConfigSet?.Invoke(this, key); }