protected virtual object ResolveOutput(RedisKvCacheItem item, Type targetType) { if (targetType.IsArray) { foreach (var t in targetType.GetInterfaces()) { if (t == typeof(ICollection <byte>)) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, item.ValueBytes.FromBase64())); } } return(JsonUtil.Deserialize(item.ValueBytes.ToUtf8String(), targetType)); } if (targetType == TYPE_STRING || targetType.IsPrimitive) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, Encoding.UTF8.GetString(item.ValueBytes))); } if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Nullable <>) && Nullable.GetUnderlyingType(targetType).IsPrimitive) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, Encoding.UTF8.GetString(item.ValueBytes))); } if (targetType == typeof(DateTime) || targetType == typeof(DateTime?)) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, Encoding.UTF8.GetString(item.ValueBytes))); } return(JsonUtil.Deserialize(Encoding.UTF8.GetString(item.ValueBytes), targetType)); }
protected virtual object ResolveOutput(MemcachedCacheItem item, Type targetType) { if (item.Flags == FLAG_JSON) { if (targetType == TYPE_STRING) { return(Encoding.UTF8.GetString(item.ValueBytes)); } return(JsonUtil.Deserialize(Encoding.UTF8.GetString(item.ValueBytes), targetType)); } else if (item.Flags == FLAG_STRING) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, Encoding.UTF8.GetString(item.ValueBytes))); } else { if (targetType == TYPE_STRING) { return(item.ValueBytes.ToBase64()); } return(TypeConversionUtil.ConvertValueIfNecessary(targetType, item.ValueBytes)); } }
protected virtual object ResolveOutput(MemoryCacheItem item, Type targetType) { if (item.ValueType == CacheValueType.Json) { if (targetType == TYPE_STRING) { return(item.StringValue); } return(JsonUtil.Deserialize(item.StringValue, targetType)); } else if (item.ValueType == CacheValueType.Plain) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, item.StringValue)); } else if (item.ValueType == CacheValueType.Binary) { if (targetType == TYPE_STRING) { return(item.BinaryValue.ToBase64()); } return(TypeConversionUtil.ConvertValueIfNecessary(targetType, item.BinaryValue)); } else { throw new Exception(); } }
private object[] GenerateArguments(ParameterInfo[] parameters, List <ArgumentDefinition> arguments) { var paramValues = new List <object>(); for (int i = 0; i < parameters.Length; i++) { var arg = arguments.Find(t => t.Index.HasValue && t.Index.Value == i); if (arg == null) { arg = arguments.Find(t => t.Name == parameters[i].Name); } if (arg != null) { paramValues.Add(TypeConversionUtil.ConvertValueIfNecessary(parameters[i].ParameterType, arg.GetValue())); } else { // use default value paramValues.Add(parameters[i].DefaultValue); } } return(paramValues.ToArray()); }
public override object GetValue() { if (this.ResolvedType != null && this.ResolvedType != this.ValueDefinition.ResolvedType) { return(TypeConversionUtil.ConvertValueIfNecessary(this.ResolvedType, this.ValueDefinition.GetValue())); } return(this.ValueDefinition.GetValue()); }
public static T ConvertTo <T>(string value, T defaultValue) { try { return((T)TypeConversionUtil.ConvertValueIfNecessary(typeof(T), value)); } catch { return(defaultValue); } }
public override object GetValue() { if (this.ResolvedType == null) { return(this.Value); } return(TypeConversionUtil.ConvertValueIfNecessary(this.ResolvedType, this.Value)); }
private object CreateInstance() { object instance = null; var watch = new Stopwatch(); watch.Start(); if (this.FactoryObject != null) { instance = this.factoryMethod.Invoke(this.FactoryObject.GetValue(), GenerateArguments(this.factoryMethod.GetParameters(), this.FactoryArgs)); } else if (this.factoryMethod != null) { instance = this.factoryMethod.Invoke(null, GenerateArguments(this.factoryMethod.GetParameters(), this.FactoryArgs)); } else if (this.ctorMethod != null) { instance = this.ctorMethod.Invoke(GenerateArguments(this.ctorMethod.GetParameters(), this.ConstructorArgs)); } else { instance = Activator.CreateInstance(this.ResolvedType); } foreach (var property in this.propertyMaps) { property.Value.SetValue(instance, TypeConversionUtil.ConvertValueIfNecessary(property.Value.PropertyType, property.Key.GetValue())); } foreach (var listener in this.listenerMaps) { listener.Value.AddEventHandler(instance, listener.Key.CreateDelegate(listener.Value.EventHandlerType)); } if (this.initMethod != null) { this.initMethod.Invoke(instance, GenerateArguments(this.initMethod.GetParameters(), this.InitializeArgs)); } watch.Stop(); if (!string.IsNullOrWhiteSpace(this.ID)) { Log.Debug($"Object '{this.ID}' created, {watch.ElapsedMilliseconds}ms time took."); } else { Log.Debug($"Anonymous object created, {watch.ElapsedMilliseconds}ms time took."); } return(instance); }
public override object GetValue() { var list = Activator.CreateInstance(this.ResolvedType); foreach (var item in this.Items) { var val = TypeConversionUtil.ConvertValueIfNecessary(this.resolvedElementType, item.GetValue()); this.addItemMethod.Invoke(list, new[] { val }); } return(list); }
public override bool CanConvertTo(Type destinationType) { if (destinationType == null) { throw new ArgumentNullException(nameof(destinationType)); } if (this.ResolvedType != null) { return(destinationType.IsAssignableFrom(this.ResolvedType)); } TypeConversionUtil.ConvertValueIfNecessary(destinationType, this.Value); return(true); }
public override T ToValue <T>() { if (this.ValueKind == JValueKind.Null) { if (typeof(T).IsValueType) { throw new JsonException($"Null cannot be converted to type {typeof(T)}"); } return(default(T)); } else { return((T)TypeConversionUtil.ConvertValueIfNecessary(typeof(T), this.Value)); } }
public virtual T ToValue <T>() { if (this.ValueKind == JValueKind.Null) { if (typeof(T).IsValueType) { throw new JsonException($"Null cannot be converted to type {typeof(T)}"); } return(default(T)); } if (this.ValueKind == JValueKind.Array || this.ValueKind == JValueKind.Object) { return(JsonUtil.Deserialize <T>(this.ToString())); } else { return((T)TypeConversionUtil.ConvertValueIfNecessary(typeof(T), this.ToString())); } }
public int Count() { var resultSet = this.Select(t => SqlAggrFunc.Count()); var queryContext = (resultSet as DbQuerySet <int>).QueryContext; var entry = new LogEntry(); entry.SessionID = this.Session.ID; entry.CommandType = CommandType.Count; var watch = new Stopwatch(); watch.Start(); try { var parameters = new List <IDataParameter>(); IList <string> selectFields = null; entry.CommandText = GetExpressionParser().Parse(queryContext, parameters, out selectFields); entry.ParseElapsed = (int)watch.ElapsedMilliseconds; var conn = this.Session.GetDbConnection(false); if (conn is System.Data.Common.DbConnection) { var db_conn = (System.Data.Common.DbConnection)conn; entry.Server = db_conn.DataSource; entry.Database = db_conn.Database; } var cmd = conn.CreateCommand(); cmd.Transaction = this.Session.GetDbTransaction(); entry.TransactionID = this.Session.GetActiveTransaction()?.ID; cmd.CommandText = entry.CommandText; foreach (IDataParameter parameter in parameters) { cmd.Parameters.Add(parameter); } var result = (int)TypeConversionUtil.ConvertValueIfNecessary(typeof(int), cmd.ExecuteScalar()); watch.Stop(); entry.TotalElapsed = (int)watch.ElapsedMilliseconds; this.Session.Database.OnWriteLog(entry); return(result); } catch (Exception ex) { watch.Stop(); entry.Message = ex.Message; entry.Exception = ex; entry.TotalElapsed = (int)watch.ElapsedMilliseconds; this.Session.Database.OnWriteLog(entry); throw ex; } }
private TEntity ReadObject(IDataReader reader) { if (reader.FieldCount == 1 && !m_IsAnonymousType) { if (!m_IsAtomicType.HasValue) { m_IsAtomicType = IsAtomicType(typeof(TEntity)); } if (m_IsAtomicType.Value) { var value = reader.GetValue(0); if (value != null && value != DBNull.Value && !typeof(TEntity).IsAssignableFrom(value.GetType())) { value = TypeConversionUtil.ConvertValueIfNecessary(typeof(TEntity), value); } return((TEntity)(value == DBNull.Value ? null : value)); } //else if (reader.GetDataTypeName(0).ToUpper() == "JSON") //{ // // TODO: ... //} } if (m_pFieldProperties == null) { var t = typeof(TEntity); m_pFieldProperties = new List <PropertyInfo>(); if (!m_IsAnonymousType) { for (int i = 0; i < reader.FieldCount; i++) { var fieldName = reader.GetName(i); var property = t.GetProperty(fieldName); if (property == null || !property.CanWrite) { throw new InvalidOperationException($"Property '{property.Name}' cannot be set on '{t.FullName}'."); } m_pFieldProperties.Add(property); } } else { for (int i = 0; i < reader.FieldCount; i++) { var fieldName = reader.GetName(i); m_pFieldProperties.Add(t.GetProperty(fieldName)); } } } if (!m_IsAnonymousType) { var entity = Activator.CreateInstance <TEntity>(); for (int i = 0; i < m_pFieldProperties.Count; i++) { var value = reader.GetValue(i); if (value != null && value != DBNull.Value && !m_pFieldProperties[i].PropertyType.IsAssignableFrom(value.GetType())) { value = TypeConversionUtil.ConvertValueIfNecessary(m_pFieldProperties[i].PropertyType, value); } m_pFieldProperties[i].SetValue(entity, value == DBNull.Value ? null : value, null); } return(entity); } else { var entity = new Dictionary <string, object>(); for (int i = 0; i < m_pFieldProperties.Count; i++) { var value = reader.GetValue(i); entity[m_pFieldProperties[i].Name] = value == DBNull.Value ? null : value; } return(JsonUtil.Deserialize <TEntity>(JsonUtil.Serialize(entity))); } }
public static T ConvertTo <T>(string value) { return((T)TypeConversionUtil.ConvertValueIfNecessary(typeof(T), value)); }
private bool InternalAdd(TEntity entity, Expression <Func <TEntity, bool> > existsCondition) { var entry = new LogEntry(); entry.SessionID = this.Session.ID; entry.CommandType = CommandType.Insert; entry.TableName = this.TableName; var watch = new Stopwatch(); watch.Start(); try { var conn = this.Session.GetDbConnection(true); if (conn is System.Data.Common.DbConnection) { var db_conn = (System.Data.Common.DbConnection)conn; entry.Server = db_conn.DataSource; entry.Database = db_conn.Database; } var connectElapsed = (int)watch.ElapsedMilliseconds; var cmd = conn.CreateCommand(); cmd.Transaction = this.Session.GetDbTransaction(); entry.TransactionID = this.Session.GetActiveTransaction()?.ID; var parameters = new List <IDataParameter>(); if (existsCondition == null) { entry.CommandText = ParseInsertSql(entity, parameters); } else { entry.CommandText = ParseInsertSql(entity, existsCondition, parameters); } entry.ParseElapsed = (int)watch.ElapsedMilliseconds - connectElapsed; cmd.CommandText = entry.CommandText; foreach (IDataParameter parameter in parameters) { cmd.Parameters.Add(parameter); } int affectedRows = cmd.ExecuteNonQuery(); if (affectedRows > 0 && IdentityProperty != null) { cmd = conn.CreateCommand(); cmd.CommandText = GetLastInsertIdSql(); var lastInsertId = TypeConversionUtil.ConvertValueIfNecessary(IdentityProperty.PropertyType, cmd.ExecuteScalar()); IdentityProperty.SetValue(entity, lastInsertId, null); } watch.Stop(); entry.TotalElapsed = (int)watch.ElapsedMilliseconds; this.Session.Database.OnWriteLog(entry); return(affectedRows > 0); } catch (Exception ex) { watch.Stop(); entry.Message = ex.Message; entry.Exception = ex; entry.TotalElapsed = (int)watch.ElapsedMilliseconds; this.Session.Database.OnWriteLog(entry); throw ex; } }