2020-07-05 00:09:53 +08:00
|
|
|
|
using System;
|
2020-08-11 15:27:15 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-07-23 15:49:23 +08:00
|
|
|
|
using System.Globalization;
|
2020-08-11 15:27:15 +08:00
|
|
|
|
using System.Linq.Expressions;
|
2020-08-10 16:05:50 +08:00
|
|
|
|
using AntDesign.Core.Reflection;
|
2020-07-05 00:09:53 +08:00
|
|
|
|
|
|
|
|
|
namespace AntDesign.Helpers
|
|
|
|
|
{
|
|
|
|
|
internal static class Formatter<T>
|
|
|
|
|
{
|
|
|
|
|
private static readonly Lazy<Func<T, string, string>> _formatFunc = new Lazy<Func<T, string, string>>(GetFormatLambda, true);
|
|
|
|
|
|
|
|
|
|
public static string Format(T source, string format)
|
|
|
|
|
{
|
|
|
|
|
return _formatFunc.Value.Invoke(source, format);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Func<T, string, string> GetFormatLambda()
|
|
|
|
|
{
|
|
|
|
|
var type = typeof(T);
|
|
|
|
|
var p1 = Expression.Parameter(typeof(T));
|
|
|
|
|
var p2 = Expression.Parameter(typeof(string));
|
|
|
|
|
|
2020-08-10 16:05:50 +08:00
|
|
|
|
Expression variable = p1;
|
2020-07-05 00:09:53 +08:00
|
|
|
|
Expression body = p2;
|
2020-08-11 15:27:15 +08:00
|
|
|
|
Expression hasValueExpression = type.IsValueType ? (Expression)Expression.Constant(true) : Expression.NotEqual(p1, Expression.Default(type));
|
2020-08-10 16:05:50 +08:00
|
|
|
|
|
|
|
|
|
if (TypeDefined<T>.IsNullable)
|
|
|
|
|
{
|
|
|
|
|
type = TypeDefined<T>.NullableType;
|
2020-08-11 15:27:15 +08:00
|
|
|
|
hasValueExpression = Expression.Equal(Expression.Property(p1, "HasValue"), Expression.Constant(true));
|
|
|
|
|
variable = Expression.Condition(hasValueExpression, Expression.Property(p1, "Value"), Expression.Default(type));
|
2020-08-10 16:05:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 00:09:53 +08:00
|
|
|
|
if (type.IsSubclassOf(typeof(IFormattable)))
|
|
|
|
|
{
|
|
|
|
|
var method = type.GetMethod("ToString", new[] { typeof(string), typeof(IFormatProvider) });
|
2020-08-10 16:05:50 +08:00
|
|
|
|
body = Expression.Call(Expression.Convert(variable, type), method, p2, Expression.Constant(null));
|
2020-07-05 00:09:53 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var method = type.GetMethod("ToString", new[] { typeof(string) });
|
|
|
|
|
if (method != null)
|
|
|
|
|
{
|
2020-08-10 16:05:50 +08:00
|
|
|
|
body = Expression.Call(Expression.Convert(variable, type), method, p2);
|
2020-07-05 00:09:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-10 16:05:50 +08:00
|
|
|
|
|
2020-08-11 15:27:15 +08:00
|
|
|
|
var condition = Expression.Condition(hasValueExpression, body, Expression.Constant(string.Empty));
|
|
|
|
|
return Expression.Lambda<Func<T, string, string>>(condition, p1, p2).Compile();
|
2020-07-05 00:09:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-23 15:49:23 +08:00
|
|
|
|
|
|
|
|
|
internal static class Formatter
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// under WASM mode, when format a double number to percentage, there will be a blank between number and %, '35.00 %'
|
|
|
|
|
/// use this method instead to avoid the blank space
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string ToPercentWithoutBlank(double num)
|
|
|
|
|
{
|
|
|
|
|
return num.ToString("p", CultureInfo.CurrentCulture).Replace(" ", "", StringComparison.Ordinal);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-05 00:09:53 +08:00
|
|
|
|
}
|