public static async Task <IDictionary <string, string> > FormatAsync(this IKeyValueFormatterFactory keyValueFormatterFactory, IKeyValueFormatter formatter, object model, Type type = null, string name = null) { var context = new KeyValueFormatterContext(keyValueFormatterFactory) { BinderModelName = name ?? string.Empty, Model = model, ModelType = type ?? model.GetType() }; await formatter.FormatAsync(context); return(context.Result); }
public async Task FormatAsync(KeyValueFormatterContext context) { var model = context.Model; if (model == null) { return; } var modelType = context.ModelType; Type elementType; if (modelType.HasElementType) { elementType = modelType.GetElementType(); } else if (modelType.IsGenericType && typeof(IEnumerable <>).IsAssignableFrom(modelType.GetGenericTypeDefinition())) { elementType = modelType.GenericTypeArguments[0]; } else { return; } var keyValueFormatter = context.FormatterFactory.CreateFormatter(elementType); if (model is IEnumerable enumerable) { var i = 0; foreach (var item in enumerable.Cast <object>()) { var itemContext = new KeyValueFormatterContext(context.FormatterFactory) { BinderModelName = context.BinderModelName + $"[{i}]", Model = item, ModelType = elementType }; await keyValueFormatter.FormatAsync(itemContext); foreach (var t in itemContext.Result) { context.Result[t.Key] = t.Value; } i++; } } }
public async Task FormatAsync(KeyValueFormatterContext context) { var code = Type.GetTypeCode(context.ModelType); if (code != TypeCode.Object) { return; } var properties = context.ModelType.GetProperties(); foreach (var property in properties) { if (property.GetTypeAttribute <PropertyIgnoreAttribute>() != null) { continue; } var binderModelName = (property.GetTypeAttribute <KeyNameAttribute>()?.Name ?? property.Name); if (!string.IsNullOrEmpty(context.BinderModelName)) { binderModelName = context.BinderModelName + "." + binderModelName; } var propertyContext = new KeyValueFormatterContext(context.FormatterFactory) { BinderModelName = binderModelName, ModelType = property.PropertyType, Model = property.GetValue(context.Model) }; var customKeyValueFormatter = property.GetTypeAttribute <IKeyValueFormatter>(); var keyValueFormatter = customKeyValueFormatter ?? context.FormatterFactory.CreateFormatter(propertyContext.ModelType); await keyValueFormatter.FormatAsync(propertyContext); foreach (var t in propertyContext.Result) { context.Result[t.Key] = t.Value; } } }
public Task FormatAsync(KeyValueFormatterContext context) { var key = context.BinderModelName; var model = context.Model; if (model == null) { return(Task.CompletedTask); } string value = null; var modelType = context.ModelType; modelType = Nullable.GetUnderlyingType(modelType) ?? modelType; switch (Type.GetTypeCode(modelType)) { case TypeCode.Boolean: value = model is bool b && b ? "true" : "false"; break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.SByte: case TypeCode.Single: case TypeCode.String: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: value = model.ToString(); break; case TypeCode.DateTime: value = model is DateTime dateTime?dateTime.ToString("yyyy-MM-dd HH:mm:ss") : null; break; case TypeCode.DBNull: value = null; break; case TypeCode.Empty: value = string.Empty; break; case TypeCode.Object: break; default: return(Task.CompletedTask); } context.Result[key] = value; return(Task.CompletedTask); }