using System.Reflection; namespace YLErp.Helpers { public static class ReflectionHelper { /// /// 用于找出属性值为double.nan的对象 /// 以帮助EF保存时能够避免出错 /// [System.Diagnostics.Conditional("DEBUG")] public static void CheckNanValue(IEnumerable objs) where T : class { CheckNanValue2(objs); } /// /// 远程调试的时候使用,程序内不要调用 /// public static void CheckNanValue2(IEnumerable 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); } } } } /// /// /// /// public static bool IsNullableType(Type t) { if (t is null) { throw new ArgumentNullException(nameof(t)); } return t.GetTypeInfo().IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>); } } }