Exemple #1
0
 public void AddParameters <T>(T obj, SqlSerializer.ParameterFlags flags = SqlSerializer.ParameterFlags.Default)
 {
     foreach (KeyValuePair <string, MappingInfo> keyValuePair in SqlSerializer.GetFieldMappings <T>())
     {
         MappingInfo mappingInfo = keyValuePair.Value;
         if ((flags & SqlSerializer.ParameterFlags.IdFieldsOnly) == SqlSerializer.ParameterFlags.IdFieldsOnly && mappingInfo.MappingAttribute.IsId && ((flags & SqlSerializer.ParameterFlags.ExcludeIdentityFields) == SqlSerializer.ParameterFlags.ExcludeIdentityFields && !mappingInfo.MappingAttribute.IsIdentity))
         {
             this._command.Parameters.AddWithValue("@" + mappingInfo.Name, mappingInfo.GetValue((object)obj));
         }
     }
 }
Exemple #2
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);
     }
 }
Exemple #3
0
 public static Dictionary <string, MappingInfo> GetFieldMappings <T>()
 {
     return(SqlSerializer.GetFieldMappings(typeof(T)));
 }