payne
2024-04-24 0632e972f30627c5bd6c5a84373bab8e54a4c3ed
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
#nullable enable
 
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
 
namespace Admin.NET.Core
{
    public static class FieldUtil
    {
        /// <summary>
        /// 获取标量属性是否必须
        /// </summary>
        /// <param name="p"></param>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static bool IsRequired(IProperty p, string dataType)
        {
            if (dataType.StartsWith("System.Nullable"))
            {
                return false;
            }
 
            if ("System.String".Equals(dataType))
            {
                return p.PropertyInfo?.IsDefined(typeof(RequiredAttribute), true) ?? false;
            }
            
            return true;
        }
 
        /// <summary>
        /// 获取导航属性是否必须
        /// </summary>
        /// <param name="p"></param>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static bool IsRequired(INavigation p, string dataType)
        {
            if (dataType.StartsWith("System.Nullable"))
            {
                return false;
            }          
            return true;
        }
 
        /// <summary>
        /// 获取导航属性是否注解信息
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static string GetComment(this INavigation p)
        {
            return ((CommentAttribute?)p?.PropertyInfo?.GetCustomAttribute(typeof(CommentAttribute), true))?.Comment ?? "";
        }
 
 
        /// <summary>
        /// 获取类型检称
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public static string GetSimpleName(this string fullName) 
        {
            var args = fullName.Split(".");
            if (args == null) return "";
            return args[^1];
        }
 
       
       
    }
}