/// <summary> /// 从字典转换为属性集合 /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TValue"></typeparam> /// <param name="keyValuePairs"></param> /// <returns></returns> public static PropertyValueCollection ToProperties <TKey, TValue>(this IEnumerable <KeyValuePair <TKey, TValue> > keyValuePairs) { PropertyValueCollection properties = new PropertyValueCollection(); foreach (KeyValuePair <TKey, TValue> kp in keyValuePairs) { if (kp.Key != null) { PropertyValue pv = BuildPropertyValue(kp.Key.ToString(), string.Empty); if (kp.Value != null) { System.Type type = kp.Value.GetType(); IObjectValueToPropertyValue converter = GetObjectValueConverter(type); if (converter != null) { pv.Definition.ReadOnly = false; converter.ConvertToPropertyValue(type, kp.Value, pv, kp); } else { pv.StringValue = kp.Value.ToString(); } pv.Definition.DefaultValue = pv.StringValue; } properties.Add(pv); } } return(properties); }
/// <summary> /// 属性集合回填到字典中 /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TValue"></typeparam> /// <param name="pvs"></param> /// <param name="dictionary"></param> public static void FillDictionary <TKey, TValue>(this PropertyValueCollection pvs, IDictionary <TKey, TValue> dictionary) { if (pvs != null && dictionary != null) { foreach (PropertyValue pv in pvs) { TValue originalObjectValue = default(TValue); TKey key = (TKey)DataConverter.ChangeType(pv.Definition.Name, typeof(TKey)); if (dictionary.TryGetValue(key, out originalObjectValue)) { if (pv.Definition.ReadOnly == false && originalObjectValue != null) { System.Type type = originalObjectValue.GetType(); IObjectValueToPropertyValue converter = GetObjectValueConverter(type); dictionary[key] = (TValue)converter.PropertyValueToObjectValue(pv, type, originalObjectValue, pvs); } } else { dictionary.Add(key, (TValue)pv.GetRealValue()); } } } }
public IObjectValueToPropertyValue GetEqualToConverter(Type type) { IObjectValueToPropertyValue result = null; if (this.ContainsKey(type)) { result = this[type].Converter; } return(result); }
public IObjectValueToPropertyValue GetIsConverter(Type type) { IObjectValueToPropertyValue result = null; foreach (ObjectValueTypeAndConverter ovtc in this) { if (ovtc.ValueType.IsAssignableFrom(type)) { result = ovtc.Converter; break; } } return(result); }
public static IObjectValueToPropertyValue GetObjectValueConverter(Type type) { IObjectValueToPropertyValue result = null; result = ObjectValueToPropertyValueSettings.GetConfig().GetTypeEqualToConvertors().GetEqualToConverter(type); if (result == null) { result = _BuiltInEqualTypeObjectValueConverters.GetEqualToConverter(type); } if (result == null) { result = ObjectValueToPropertyValueSettings.GetConfig().GetTypeIsConvertors().GetIsConverter(type); } if (result == null) { result = _BuiltInIsTypeObjectValueConverters.GetIsConverter(type); } return(result); }
public TypeAndConverter(Type valueType, IObjectValueToPropertyValue converter) { this.ValueType = valueType; this.Converter = converter; }