ReflectHelper.cs 4.42 KB
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;
        }
    }
}