/// <summary> /// 向字典添加值 /// </summary> public bool Add(object key, object value) { bool b; var k = _keyConvertor.ChangeType(_context, key, _keyConvertor.OutputType, out b); if (b == false) { _context.AddException($"添加到字典{CType.GetFriendlyName(_type)}失败"); return(false); } var v = _valueConvertor.ChangeType(_context, value, _valueConvertor.OutputType, out b); if (b == false) { _context.AddException($"向字典{CType.GetFriendlyName(_type)}中添加元素 {key} 失败"); return(false); } try { Instance.Add(k, v); return(true); } catch (Exception ex) { _context.AddException($"向字典{CType.GetFriendlyName(_type)}中添加元素 {key} 失败,原因:{ex.Message}", ex); return(false); } }
/// <summary> /// 设置对象值 /// </summary> /// <param name="value"> 待设置的值 </param> /// <returns> </returns> public bool Set(object value) { try { Instance.Add(value); return(true); } catch (Exception ex) { _context.AddException($"向集合{CType.GetFriendlyName(_type)}中添加第[{Instance?.Count}]个元素失败,原因:{ex.Message}", ex); return(false); } }
public bool Add(object key, object value) { try { Instance.Add(key, value); return(true); } catch (Exception ex) { _context.AddException($"向字典{CType.GetFriendlyName(_type)}中添加元素 {key} 失败,原因:{ex.Message}", ex); return(false); } }
/// <summary> /// 设置对象值 /// </summary> /// <param name="obj"> 待设置的值 </param> /// <returns> </returns> public bool Set(object obj) { bool b; var v = _convertor.ChangeType(_context, obj, _convertor.OutputType, out b); if (b == false) { _context.AddException($"向集合{CType.GetFriendlyName(_type)}中添加第[{Instance?.Count}]个元素失败"); return(false); } try { Instance.Add(v); return(true); } catch (Exception ex) { _context.AddException($"向集合{CType.GetFriendlyName(_type)}中添加第[{Instance?.Count}]个元素失败,原因:{ex.Message}", ex); return(false); } }
/// <summary> /// 返回指定类型的对象,其值等效于指定对象。 /// </summary> /// <param name="context"> </param> /// <param name="input"> 需要转换类型的对象 </param> /// <param name="outputType"> 换转后的类型 </param> /// <param name="success"> 是否成功 </param> protected override string ChangeType(ConvertContext context, object input, Type outputType, out bool success) { success = true; if (input is DataRow || input is DataRowView) { return(ComponentServices.ToJsonString(input)); } var reader = input as IDataReader; if (reader != null) { if (reader.IsClosed) { success = false; context.AddException("DataReader已经关闭"); return(null); } switch (reader.FieldCount) { case 0: return(null); case 1: return(BaseChangeType(context, reader.GetValue(0), outputType, out success)); default: return(ComponentServices.ToJsonString(input)); } } if (input.IsNull()) { return(null); } if (input is bool) { return((bool)input ? "true" : "false"); } var convertible = input as IConvertible; if (convertible != null) { return(convertible.ToString(null)); } var formattable = input as IFormattable; if (formattable != null) { return(formattable.ToString(null, null)); } var type = input as Type; if (type != null) { return(CType.GetFriendlyName(type)); } var bs = input as byte[]; if (bs != null) { return(Encoding.UTF8.GetString(bs)); } var ps = input.GetType().GetProperties(); if (ps.Length > 0) { return(ComponentServices.ToJsonString(input)); } return(input.ToString()); }