예제 #1
0
 private static object Deserialize(IDataRecord reader, Type type, bool dateTimeAsUtc = false)
 {
     if (type == typeof(ExpandoObject))
     {
         ExpandoObject expandoObject             = new ExpandoObject();
         IDictionary <string, object> dictionary = (IDictionary <string, object>)expandoObject;
         for (int i = 0; i < reader.FieldCount; ++i)
         {
             dictionary.Add(reader.GetName(i), reader[i]);
         }
         return((object)expandoObject);
     }
     else
     {
         if (reader.GetFieldType(0) == type)
         {
             return(reader.GetValue(0));
         }
         object instance = Activator.CreateInstance(type);
         Dictionary <string, MappingInfo> fieldMappings = SqlSerializer.GetFieldMappings(type);
         for (int i = 0; i < reader.FieldCount; ++i)
         {
             if (fieldMappings.ContainsKey(reader.GetName(i)))
             {
                 MappingInfo mappingInfo = fieldMappings[reader.GetName(i)];
                 object      obj         = reader[i];
                 if (obj != null && obj != DBNull.Value)
                 {
                     if (obj is DateTime)
                     {
                         if (dateTimeAsUtc)
                         {
                             obj = (object)DateTime.SpecifyKind((DateTime)obj, DateTimeKind.Utc);
                         }
                     }
                     try
                     {
                         mappingInfo.SetValue(instance, obj);
                     }
                     catch (Exception ex)
                     {
                         throw new ArgumentException(string.Format("Failed to map Type: {0}", (object)type), mappingInfo.Name, ex);
                     }
                 }
             }
         }
         return(instance);
     }
 }