1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Reflection;
 
namespace CSFramework.DB
{
    internal static class CommonMethods
    {
        internal static DateTime MinSqlDate =>
            DateTime.Parse("1901-01-01 00:00");
 
        /// <summary>
        /// 转换为SQL支持的日期格式, 日期范围:1753-1-1 ~ 9999-12-31
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        internal static DateTime ToSqlDateTime(object o)
        {
            var defMinValue = DateTime.Parse("1753-01-01 00:00:00");
            var defMaxValue = DateTime.Parse("9999-12-31 23:59:59");
            if (null == o) return defMinValue; //传入空值,返回预设值
 
            DateTime dt;
            if (DateTime.TryParse(o.ToString(), out dt))
            {
                if (dt < defMinValue || dt > defMaxValue)
                    return defMinValue; //无效日期,预设返回SQL支持的最小日期
                else
                    return dt;
            }
 
            return defMinValue;
        }
 
        /// <summary>
        /// 转换为decimal类型(有效十进制数)
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static decimal ToDecimal(object o)
        {
            if (null == o) return 0;
            try
            {
                return Convert.ToDecimal(o.ToString());
            }
            catch
            {
                return 0;
            }
        }
 
        /// <summary>
        /// 获取对象的属性和成员变量
        /// </summary>
        /// <param name="T"></param>
        /// <returns></returns>
        internal static FieldInfo[] GetAllInfos(Type T)
        {
            FieldInfo[] infos = null;
            var infosSelf = T.GetFields();
            FieldInfo[] infosBase = null;
 
            if (T.BaseType != null)
                infosBase = GetAllInfos(T.BaseType);
 
            if (infosBase == null || infosBase.Length == 0)
            {
                infos = infosSelf;
            }
            else
            {
                var iLength = infosBase.Length;
                Array.Resize<FieldInfo>(ref infosBase,
                    infosBase.Length + infosSelf.Length);
                infosSelf.CopyTo(infosBase, iLength);
                infos = infosBase;
            }
 
            return infos;
        }
    }
}