From d84300651d256f684d552b1db7a1c4fd9ac9ef57 Mon Sep 17 00:00:00 2001 From: James Yeung Date: Mon, 10 Aug 2020 16:05:50 +0800 Subject: [PATCH] fix(module: table): formatting with nullable type for table column (#487) --- components/core/Helpers/Formatter.cs | 14 +++++++-- components/core/Reflection/TypeDefined.cs | 29 +++++++++++++++++++ .../Demos/Components/Table/demo/Blazor.razor | 2 +- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 components/core/Reflection/TypeDefined.cs diff --git a/components/core/Helpers/Formatter.cs b/components/core/Helpers/Formatter.cs index d88c4d2e..a984c404 100644 --- a/components/core/Helpers/Formatter.cs +++ b/components/core/Helpers/Formatter.cs @@ -1,6 +1,8 @@ using System; using System.Linq.Expressions; using System.Globalization; +using AntDesign.Core.Reflection; +using System.Collections.Generic; namespace AntDesign.Helpers { @@ -19,20 +21,28 @@ namespace AntDesign.Helpers var p1 = Expression.Parameter(typeof(T)); var p2 = Expression.Parameter(typeof(string)); + Expression variable = p1; Expression body = p2; + + if (TypeDefined.IsNullable) + { + type = TypeDefined.NullableType; + } + if (type.IsSubclassOf(typeof(IFormattable))) { var method = type.GetMethod("ToString", new[] { typeof(string), typeof(IFormatProvider) }); - body = Expression.Call(Expression.Convert(p1, type), method, p2, Expression.Constant(null)); + body = Expression.Call(Expression.Convert(variable, type), method, p2, Expression.Constant(null)); } else { var method = type.GetMethod("ToString", new[] { typeof(string) }); if (method != null) { - body = Expression.Call(Expression.Convert(p1, type), method, p2); + body = Expression.Call(Expression.Convert(variable, type), method, p2); } } + return Expression.Lambda>(body, p1, p2).Compile(); } } diff --git a/components/core/Reflection/TypeDefined.cs b/components/core/Reflection/TypeDefined.cs new file mode 100644 index 00000000..4d59c81e --- /dev/null +++ b/components/core/Reflection/TypeDefined.cs @@ -0,0 +1,29 @@ +using System; + +namespace AntDesign.Core.Reflection +{ + internal static class TypeDefined + { + public static bool IsNullable; + + public static bool IsGenericType => typeof(T).IsGenericType; + + public static Type NullableType; + + static TypeDefined() + { + IsNullable = IsNullableType(typeof(T)); + NullableType = GetNullableGenericType(typeof(T)); + } + + private static bool IsNullableType(Type type) + { + return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); + } + + private static Type GetNullableGenericType(Type type) + { + return IsNullableType(type) ? type.GetGenericArguments()[0] : null; + } + } +} diff --git a/site/AntBlazor.Docs/Demos/Components/Table/demo/Blazor.razor b/site/AntBlazor.Docs/Demos/Components/Table/demo/Blazor.razor index 5d1b85cf..183c427c 100644 --- a/site/AntBlazor.Docs/Demos/Components/Table/demo/Blazor.razor +++ b/site/AntBlazor.Docs/Demos/Components/Table/demo/Blazor.razor @@ -74,7 +74,7 @@ public int Id { get; set; } [DisplayName("Date")] - public DateTime Date { get; set; } + public DateTime? Date { get; set; } [DisplayName("Temp. (C)")] public int TemperatureC { get; set; }