using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; namespace HT.Cloud.Code { public class ReflectionHelper { private static ConcurrentDictionary dictCache = new ConcurrentDictionary(); private static List exceptionList = new List { "BaseService", "BaseController" }; #region 得到类里面的属性集合 /// /// 得到类里面的属性集合 /// /// /// /// public static PropertyInfo[] GetProperties(Type type, string[] columns = null) { PropertyInfo[] properties = null; if (dictCache.ContainsKey(type.FullName)) { properties = dictCache[type.FullName] as PropertyInfo[]; } else { properties = type.GetProperties(); dictCache.TryAdd(type.FullName, properties); } if (columns != null && columns.Length > 0) { // 按columns顺序返回属性 var columnPropertyList = new List(); foreach (var column in columns) { var columnProperty = properties.Where(p => p.Name == column).FirstOrDefault(); if (columnProperty != null) { columnPropertyList.Add(columnProperty); } } return columnPropertyList.ToArray(); } else { return properties; } } public static object GetObjectPropertyValue(T t, string propertyname) { Type type = typeof(T); PropertyInfo property = type.GetProperty(propertyname); if (property == null) return string.Empty; object o = property.GetValue(t, null); if (o == null) return string.Empty; return o; } #endregion 得到类里面的属性集合 /// /// StackTrace获取名称 /// /// 搜索层级 /// 前缀 /// public static string GetModuleName(int count = 10, bool isReplace = true, string prefix = "Service") { try { string moduleName = ""; for (int i = 0; i < count; i++) { if (!string.IsNullOrEmpty(moduleName)) { break; } string className = new StackFrame(i, true).GetMethod().DeclaringType.FullName; className = className.Split('+')[0]; className = className.Split('.').LastOrDefault(); bool skip = false; foreach (var item in exceptionList) { if (className.Contains(item)) { skip = true; break; } } if (skip) { continue; } if (className.IndexOf(prefix) > -1) { moduleName = className; if (isReplace) { moduleName = moduleName.Replace(prefix, ""); } } } return moduleName; } catch (Exception ex) { LogHelper.WriteWithTime(ex); return ""; } } } }