74 lines
2.1 KiB
C#
74 lines
2.1 KiB
C#
using System.Reflection;
|
|
|
|
namespace YLErp.Helpers
|
|
{
|
|
public static class ReflectionHelper
|
|
{
|
|
/// <summary>
|
|
/// 用于找出属性值为double.nan的对象
|
|
/// 以帮助EF保存时能够避免出错
|
|
/// </summary>
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
|
public static void CheckNanValue<T>(IEnumerable<T> objs) where T : class
|
|
{
|
|
CheckNanValue2(objs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 远程调试的时候使用,程序内不要调用
|
|
/// </summary>
|
|
public static void CheckNanValue2<T>(IEnumerable<T> objs) where T : class
|
|
{
|
|
if (objs is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(objs));
|
|
}
|
|
|
|
var props = typeof(T).GetTypeInfo().GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
|
.Where(n => n.CanRead && (n.PropertyType == typeof(double) || n.PropertyType == typeof(double?))).ToArray();
|
|
|
|
foreach (var obj in objs)
|
|
{
|
|
foreach (var p in props)
|
|
{
|
|
var dobj = p.GetValue(obj);
|
|
|
|
if (dobj == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
double dd;
|
|
|
|
if (dobj is double?)
|
|
{
|
|
dd = ((double?)dobj).Value;
|
|
}
|
|
else
|
|
{
|
|
dd = (double)dobj;
|
|
}
|
|
|
|
if (double.IsNaN(dd))
|
|
{
|
|
throw new Exception("找到一个NaN:" + p.Name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="t"></param>
|
|
public static bool IsNullableType(Type t)
|
|
{
|
|
if (t is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(t));
|
|
}
|
|
return t.GetTypeInfo().IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>);
|
|
}
|
|
}
|
|
}
|