using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace EGFramework{ public static class EGenerateVariant { public static Dictionary EGenerateDictiontaryByType(this Type self) { PropertyInfo[] propertyNames = self.GetProperties(); FieldInfo[] fieldNames = self.GetFields(); Dictionary result = new Dictionary(); foreach (PropertyInfo pName in propertyNames) { result.Add(pName.Name, pName.Name); } foreach (FieldInfo fName in fieldNames) { result.Add(fName.Name, fName.Name); } return result; } public static Dictionary EGenerateEmptyDictiontaryByType(this Type self) { PropertyInfo[] propertyNames = self.GetProperties(); FieldInfo[] fieldNames = self.GetFields(); Dictionary result = new Dictionary(); foreach (PropertyInfo pName in propertyNames) { result.Add(pName.Name, ""); } foreach (FieldInfo fName in fieldNames) { result.Add(fName.Name, ""); } return result; } public static Dictionary EGenerateTypeDictiontaryByType(this Type self) { PropertyInfo[] propertyNames = self.GetProperties(); FieldInfo[] fieldNames = self.GetFields(); Dictionary result = new Dictionary(); foreach (PropertyInfo pName in propertyNames) { result.Add(pName.Name, pName.PropertyType); } foreach (FieldInfo fName in fieldNames) { result.Add(fName.Name, fName.FieldType); } return result; } public static Dictionary EGenerateDictiontaryByObject(this T self) { PropertyInfo[] propertyNames = typeof(T).GetProperties(); FieldInfo[] fieldNames = typeof(T).GetFields(); // object[] s = propertyNames.Select(p => p.GetValue(self)).ToArray(); // object[] a = fieldNames.Select(p => p.GetValue(self)).ToArray(); Dictionary result = new Dictionary(); foreach (PropertyInfo pName in propertyNames) { object p = pName.GetValue(self); result.Add(pName.Name, p); } foreach (FieldInfo fName in fieldNames) { object p = fName.GetValue(self); result.Add(fName.Name, p); } return result; } public static List> EGenerateDictionaryByGroup(this IEnumerable self) { List> result = new List>(); PropertyInfo[] propertyNames = typeof(T).GetProperties(); FieldInfo[] fieldNames = typeof(T).GetFields(); foreach (T member in self) { Dictionary mResult = new Dictionary(); foreach (PropertyInfo pName in propertyNames) { object p = pName.GetValue(member); mResult.Add(pName.Name, p); } foreach (FieldInfo fName in fieldNames) { object p = fName.GetValue(member); mResult.Add(fName.Name, p); } result.Add(mResult); } return result; } //Default primary key is id,Id,ID. public static string EGetDefaultPrimaryKey(this Dictionary self) { foreach (KeyValuePair param in self) { if (param.Key == "ID" || param.Key == "Id" || param.Key == "id") { return param.Key; } } return ""; } public static List> ESearchByKeyword(this List> data, string fieldName, string keyWords) { List> result = new List>(); foreach (Dictionary tablerow in data) { if (tablerow.ContainsKey(fieldName)) { if (tablerow[fieldName].ToString().Contains(keyWords)) { result.Add(tablerow); } } } return result; } } }