ReflectHelper.cs
4.42 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Hh.Mes.Common.Reflect
{
public static class ReflectHelper
{
private static readonly List<string> MemberName = new List<string>() { "IsPrimaryKey", "IsIgnore" };
public static string GetExpressions<T>(T t, string tableName)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(" 1=1 ");
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
for (int i = 0; i < propertys.Length; i++)
{
#region 自定义属性 IsPrimaryKey ,IsIgnore 忽略
var customAttributes = propertys[i].CustomAttributes;
var isOk = false;
foreach (var temp in customAttributes)
{
var namedArguments = temp.NamedArguments;
if (namedArguments == null || namedArguments.Count == 0) continue;
if (MemberName.Any(x => x == namedArguments[0].MemberName))
{
isOk = true;
break;
}
}
if (isOk) continue;
#endregion
object value = propertys[i].GetValue(t, null);
if (value == null || value.ToString() == "") continue;
if (propertys[i].PropertyType == typeof(int) || propertys[i].PropertyType == typeof(int?) ||
propertys[i].PropertyType == typeof(short) || propertys[i].PropertyType == typeof(short?) ||
propertys[i].PropertyType == typeof(long) || propertys[i].PropertyType == typeof(long?) ||
propertys[i].PropertyType == typeof(decimal) || propertys[i].PropertyType == typeof(decimal?) ||
propertys[i].PropertyType == typeof(double) || propertys[i].PropertyType == typeof(double?)
)
{
//默认id为0的 设置-11111 不考虑
if (!value.Equals(-11111))
{
stringBuilder.Append($" and {tableName}.{propertys[i].Name} like '%{value}%' ");
}
}
else if (propertys[i].PropertyType == typeof(string))
{
stringBuilder.Append($" and {tableName}.{propertys[i].Name} like '%{value}%' ");
}
}
return stringBuilder.ToString();
}
public static List<Type> GetTypes<T>() where T : class
{
var listOfBs = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
from assemblyType in domainAssembly.GetTypes()
where typeof(T).IsAssignableFrom(assemblyType)
select assemblyType).ToList();
return listOfBs;
}
public static Dictionary<string, string> GetDescriptions<T>(T entity) where T : class
{
var description = new Dictionary<string, string>();
foreach (var item in entity.GetType().GetProperties())
{
var displayName = item.GetCustomAttribute<DescriptionAttribute>();
if (displayName != null)
{
description.Add(item.Name, displayName.ToString());
}
else
{
description.Add(item.Name, item.Name);
}
}
return description;
}
public static Dictionary<string, Type> GetControlAreas<T>() where T : class
{
var result = new Dictionary<string, Type>();
var controls = GetTypes<T>().Where(x => !string.IsNullOrWhiteSpace(x.GetCustomAttribute<AreaAttribute>()?.ToString()));
foreach (var control in controls)
{
var areaName = control.GetCustomAttribute<AreaAttribute>().RouteValue?.ToString();
//areaName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(areaName);
if (!result.ContainsKey(areaName))
{
result.Add(areaName, control);
}
}
return result;
}
}
}