/// <summary> /// 將 DataReader 的資料轉換成自訂 Class 的 object /// </summary> public static object ToTypeObject(this IDataReader dr, Type type, bool caseSensitive = false) { if (dr != null) { object model = type.Assembly.CreateInstance(type.FullName); var props = type.GetProperties(); var names = Enumerable.Range(0, dr.FieldCount) .Select(i => caseSensitive ? dr.GetName(i) : dr.GetName(i).ToLower()); foreach (PropertyInfo prop in props) { string columnName = CSharpHelper.GetColumnName(prop); if (names.Contains(caseSensitive ? columnName : columnName.ToLower()) && dr[columnName] != DBNull.Value) { CSharpHelper.SetValueToProperty(model, prop, dr[columnName]); } else { Dictionary <string, object> values = dr.ToDictionary() .Where(x => x.Key.StartsWith($"{columnName}.")) .ToDictionary( x => x.Key.Substring(columnName.Length + 1), x => x.Value); if (values.Any()) { CSharpHelper.SetValueToProperty(model, prop, values.ToTypeObject(prop.PropertyType)); } } } return(model); } return(null); }
/// <summary> /// 將自訂類型的 Properties 轉換成 Dictionary /// </summary> public static Dictionary <string, object> ToDictionary <T>(this T model, bool isComponentModel = false) { Dictionary <string, object> properties = new Dictionary <string, object>(); var props = isComponentModel ? CSharpHelper.GetMappedProperties <T>() : model.GetType().GetProperties(); foreach (PropertyInfo prop in props) { properties.Add( isComponentModel ? CSharpHelper.GetColumnName(prop) : prop.Name, prop.GetValue(model)); } return(properties); }
/// <summary> /// 將自訂類型的 Properties 轉換成 Dictionary, /// 轉換可依照 Attribute 來執行自定義的處理邏輯 /// </summary> public static Dictionary <string, object> ToDictionary <T, TAttr>(this T model, Func <object, IEnumerable <TAttr>, object> attrAdapter, bool isComponentModel = false) where TAttr : Attribute { Dictionary <string, object> properties = new Dictionary <string, object>(); var props = isComponentModel ? CSharpHelper.GetMappedProperties <T>() : model.GetType().GetProperties(); foreach (PropertyInfo prop in props) { string key = isComponentModel ? CSharpHelper.GetColumnName(prop) : prop.Name; object value = prop.GetValue(model); IEnumerable <TAttr> attrs = prop.GetCustomAttributes <TAttr>(); if (attrs.Any()) { value = attrAdapter(value, attrs); } properties.Add(key, value); } return(properties); }
/// <summary> /// 將 Dictionary 轉換成自訂 Class 的 object /// </summary> public static object ToTypeObject <T>(this Dictionary <string, T> dictionary, Type type, bool caseSensitive = false) { if (dictionary != null) { object model = type.Assembly.CreateInstance(type.FullName); var props = model.GetType().GetProperties(); var names = dictionary.Keys.ToArray(); foreach (PropertyInfo prop in props) { string columnName = CSharpHelper.GetColumnName(prop); int i = caseSensitive ? Array.IndexOf(names, columnName) : Array.IndexOf( names.Select(x => x.ToLower()).ToArray(), columnName.ToLower()); if (i >= 0 && dictionary[names[i]] != null && (dictionary[names[i]] as object) != DBNull.Value) { CSharpHelper.SetValueToProperty(model, prop, dictionary[names[i]]); } else { Dictionary <string, T> values = dictionary .Where(x => x.Key.StartsWith($"{columnName}.")) .ToDictionary( x => x.Key.Substring(columnName.Length + 1), x => x.Value); if (values.Any()) { CSharpHelper.SetValueToProperty(model, prop, values.ToTypeObject(prop.PropertyType)); } } } return(model); } return(null); }