0

我有一个自定义的可编辑ConfigurationElement,通常我将它用作我的一些类的设置对象。(通过构造函数传递它)

当我从具有自己的设置属性的外部 dll 中获得一个对象时(因此我无法将其更改为直接从我的配置元素中读取),我正在使用此扩展将配置属性值复制到对象属性中:

/// <summary>
/// Set the object properties from a configuration element including the unrecognized attributes.
/// </summary>
/// <typeparam name="T">The object type</typeparam>
/// <param name="obj">The object to set</param>
/// <param name="configElement">The configuration element to take the properties from</param>
/// <returns></returns>
public static T SetProperties<T>(this T obj, BaseConfigurationElement configElement) => obj.SetProperties(configElement.GetProperties(true));

public static T SetProperties<T>(this T obj, object properties) => SetProperties(obj, properties?.GetType().GetProperties().ToDictionary(p => p.Name, p => p.GetValue(properties)));
public static T SetProperties<T>(this T obj, Dictionary<string, string> properties) => SetProperties(obj, properties.ToDictionary(i => i.Key, i => i.Value as object));
public static T SetProperties<T>(this T obj, Dictionary<string, object> properties)
{
    if (obj != null && properties != null)
        foreach (PropertyInfo pi in obj.GetType().GetProperties())
            if (properties.Keys.Contains(pi.Name) && pi.CanWrite)
                try // Convert value to property type.
                {
                    object valueToSet = properties[pi.Name];

                    if (pi.PropertyType.IsEnum)
                        pi.SetValue(obj, Enum.Parse(pi.PropertyType, valueToSet.ToString()));
                    else pi.SetValue(obj, Convert.ChangeType(valueToSet, pi.PropertyType), null);
                }
                catch (Exception ex) { Logging.WriteError($"Can't convert from type [{GetTypeName(properties[pi.Name])}] to type [{pi.PropertyType.Name}] for property [{pi.Name}] of object type [{GetTypeName(obj)}]: {ex.Message}"); }

    return obj;
}

关键是我想让实时更改配置成为可能,但我没有在 ConfigurationElement 上发生值更改时发生的事件,因此我可以重新复制更改的属性。

有没有办法在我的自定义 ConfigurationElement 上为此创建事件?

Ps 我不想使用INotifyPropertyChanged接口,因为在每个属性中添加调用会非常麻烦。我在问因为 ConfigurationElement 有它的索引器,所以在这个基类上可能有一种我不知道的方法。

4

1 回答 1

0

我发现SetPropertyValue受保护的方法没有被标记为虚拟,如果它是虚拟的,它可能会更容易。

我的解决方案是使用“new”关键字隐藏索引器,它不像覆盖但没关系,因为索引器不是公共的,所以在继承自定义类时,您通常不会将自己转换为基本类型。

那应该工作:

public class PropertyChangedEventArgs : EventArgs
{
    public ConfigurationProperty Property { get; }
    public object Value { get; }
    public PropertyChangedEventArgs(ConfigurationProperty property, object value) { Property = property; Value = value; }
}

public abstract class BaseConfigurationElement : ConfigurationElement
{
    protected new Object this[ConfigurationProperty prop]
    {
        get => base[prop];
        set
        {
            base[prop] = value;
            OnPropertyChanged(new PropertyChangedEventArgs(prop, value)); // Must be arise only after the base indexer was set, because an exception may will be thrown from the indexer.
        }
    }

    protected new Object this[String propertyName]
    {
        get => base[propertyName];
        set
        {
            base[propertyName] = value;
            OnPropertyChanged(new PropertyChangedEventArgs(Properties[propertyName], value));
        }
    }

    public event EventHandler<PropertyChangedEventArgs> PropertyChanged;

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);
}
于 2021-03-09T11:04:13.497 回答