using System; using System.Collections.Generic; using System.ComponentModel; namespace DynamicDescriptors; /// /// A dictionary-backed implementation of . /// internal sealed class DictionaryPropertyDescriptor : PropertyDescriptor { /// /// A dictionary mapping property names to property values. /// private readonly IDictionary _data; /// /// The name of the property. /// private readonly string _propertyName; /// /// The type of the property. /// private readonly Type _propertyType; /// /// Initializes a new instance of the class. /// /// The dictionary that backs this property descriptor. /// The name of the property. /// The type of the property. public DictionaryPropertyDescriptor(IDictionary data, string propertyName, Type propertyType) : base(Preconditions.CheckNotNullOrEmpty(propertyName, nameof(propertyName)), null) { _data = data ?? throw new ArgumentNullException(nameof(data)); _propertyName = propertyName; _propertyType = propertyType ?? throw new ArgumentNullException(nameof(propertyType)); } /// /// Returns a value indicating returns whether resetting an object changes its value. /// /// The component to test for reset capability. /// true if resetting the component changes its value; otherwise, false. public override bool CanResetValue(object component) { return false; } /// /// Gets the type of the component this property is bound to. /// public override Type ComponentType => null; /// /// Returns the current value of the property on a component. /// /// /// The component with the property for which to retrieve the value. /// /// The value of a property for a given component. public override object GetValue(object component) { return _data[_propertyName]; } /// /// Gets a value indicating whether this property is read-only. /// public override bool IsReadOnly => false; /// /// Gets the type of the property. /// public override Type PropertyType => _propertyType; /// /// Resets the value for this property of the component to the default value. /// /// /// The component with the property value that is to be reset to the default value. /// public override void ResetValue(object component) { _data[_propertyName] = null; OnValueChanged(component, EventArgs.Empty); } /// /// Sets the value of the component to a different value. /// /// /// The component with the property value that is to be set. /// /// /// The new value. /// public override void SetValue(object component, object value) { _data[_propertyName] = value; OnValueChanged(component, EventArgs.Empty); } /// /// Determines a value indicating whether the value of this property needs to be /// persisted. /// /// /// The component with the property to be examined for persistence. /// /// true if the property should be persisted; otherwise, false. public override bool ShouldSerializeValue(object component) { return false; } }