Exemple #1
0
 /// <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);
 }
Exemple #2
0
        /// <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);
        }