/// <summary> /// 复杂类型为键值对 /// </summary> /// <param name="instance">实例</param> /// <param name="options">选项</param> /// <returns></returns> private IEnumerable <KeyValuePair <string, string> > FormatAsComplex(object instance, FormatOptions options) { return (from p in PropertyDescriptor.GetProperties(instance.GetType()) where p.IsSupportGet && p.IgnoreSerialized == false let value = p.GetValue(instance) let opt = options.CloneChange(p.DateTimeFormat) select this.FormatAsSimple(p.AliasName, value, opt)); }
/// <summary> /// 解析context的Data的属性 /// 返回多个属性组成的ConvertContext /// </summary> /// <param name="context">转换上下文</param> /// <returns></returns> protected IEnumerable <ConvertContext> GetPropertiesContexts(ConvertContext context) { return (from p in PropertyDescriptor.GetProperties(context.DataType) where p.IsSupportGet && p.IgnoreSerialized == false let value = p.GetValue(context.Data) where (p.IgnoreWhenNull && value == null) == false let options = context.Options.CloneChange(p.DateTimeFormat) select new ConvertContext(p.Name, value, context.Depths, options)); }
/// <summary> /// 执行转换 /// </summary> /// <param name="context">转换上下文</param> /// <returns></returns> public override IEnumerable <KeyValuePair <string, string> > Invoke(ConvertContext context) { // 无条件解析为属性 // 因为其它转换器都无法解析此类型 return (from p in PropertyDescriptor.GetProperties(context.Type) where p.IsSupportGet && p.IgnoreSerialized == false let value = p.GetValue(context.Value) let options = context.Options.CloneChange(p.DateTimeFormat) select this.GetKeyValuePair(p.Name, value, options)); // 只拆解第一层属性则不用递归 }
/// <summary> /// 执行转换 /// </summary> /// <param name="context">转换上下文</param> /// <returns></returns> public override IEnumerable <KeyValuePair <string, string> > Invoke(ConvertContext context) { var options = context.Options; if (options == null) { options = new FormatOptions(); } return (from p in PropertyDescriptor.GetProperties(context.Descriptor.Type) where p.IsSupportGet && p.IgnoreSerialized == false let value = p.GetValue(context.Value) let opt = options.CloneChange(p.DateTimeFormat) select this.ToKeyValuePair(p.AliasName, value, opt)); // 只拆解第一层 }