protected internal object this[OptionConfigurationProperty property] { get { if (property == null) { throw new ArgumentNullException("property"); } object value; if (_values.TryGetValue(property, out value)) { return(value); } return(property.DefaultValue); } set { this.SetPropertyValue(property, value); //激发“PropertyChanged”事件 this.RaisePropertyChanged(property.Name); } }
private void SetPropertyValue(OptionConfigurationProperty property, object value) { if (property == null) { throw new ArgumentNullException("property"); } if (object.Equals(value, property.DefaultValue)) { _values.Remove(property); } else { if (TypeExtension.IsAssignableFrom(property.Type, value.GetType())) { _values[property] = value; } else { if (property.Converter == null) { _values[property] = Converter.ConvertValue(value, property.Type, property.DefaultValue); } else { _values[property] = property.Converter.ConvertFrom(value); } } } }
private static OptionConfigurationProperty CreateOptionPropertyFromAttribute(PropertyInfo propertyInfo) { OptionConfigurationProperty result = null; var attribute = (OptionConfigurationPropertyAttribute)propertyInfo.GetCustomAttribute(typeof(OptionConfigurationPropertyAttribute)); if (attribute != null) { result = new OptionConfigurationProperty(propertyInfo); } return(result); }
private OptionConfigurationElement EnsureElementPropertyValue(OptionConfigurationProperty property) { if (property == null) { throw new ArgumentNullException("property"); } object value = null; lock (_values) { _values.TryGetValue(property, out value); if (value == null) { //如果当前配置属性定义项的类型不是一个选项配置元素,则抛出异常。因为所有复合类型必须从选项配置元素继承 if (!TypeExtension.IsAssignableFrom(typeof(OptionConfigurationElement), property.Type)) { throw new OptionConfigurationException(); } //根据配置属性定义项的类型创建其对应的目标对象 if (property.Type.IsGenericType() && property.Type.GetGenericTypeDefinition() == typeof(OptionConfigurationElementCollection <>)) { value = Activator.CreateInstance(property.Type, new object[] { property.ElementName, null }); } else { value = Activator.CreateInstance(property.Type); } //设置创建的选项配置元素对象的表示其自身的属性定义项 ((OptionConfigurationElement)value)._elementProperty = property; //将创建的目标对象保存到对应的属性值中 _values[property] = value; } } return((OptionConfigurationElement)value); }