private static void CopyBySource(IDictionary <string, string> src, object dest, bool throwOnNotExist) { IReflector r = Reflector.Bind(dest); foreach (var key in src.Keys) { if (!r.ExistProperty(key)) { if (throwOnNotExist) { ExceptionHelper.ThrowDestPropertyNotExist(dest.GetType(), key); } else { _log.Warning("Destination type [{0}] does not have property [{1}].", dest.GetType(), key); } } else { try { Type pType = r.GetPropertyType(key); object val = TypeCast.ChangeToTypeOrNullableType(src[key], pType); r.SetPropertyValue(key, val); } catch (Exception ex) { throw ex.CreateWrapException <CopyException>(); } } } }
private static void CopyByDest(IDictionary <string, string> src, object dest, bool throwOnNotExist) { IReflector r = Reflector.Bind(dest); foreach (var property in r.FindAllPropertyNames()) { if (src.ContainsKey(property)) { try { Type pType = r.GetPropertyType(property); object val = TypeCast.ChangeToTypeOrNullableType(src[property], pType); r.SetPropertyValue(property, val); } catch (Exception ex) { throw ex.CreateWrapException <CopyException>(); } } else { if (throwOnNotExist) { ExceptionHelper.ThrowSourceKeyOrPropertyNotExist(property); } else { _log.Warning("Source dictionary does not have key [{0}].", property); } } } }
public static object Build(string val, Type targetType, IObjectBuildContext ctx) { //Note:If it is injection, save it in to ctx.InjectedParameters if (string.IsNullOrEmpty(val)) { object result = ctx.Kernal.Lookup(targetType); ctx.InjectedParameters.Add(result); return(result); } if (val.BracketedBy(StringPair.Template_Bracket)) { string id = val.UnBracketing(StringPair.Template_Bracket); if (id == NULL) { return(null); } object result = ctx.Kernal.Lookup(id); ctx.InjectedParameters.Add(result); return(result); } else { return(TypeCast.ChangeToTypeOrNullableType(val, targetType)); } }
public void TestConvertNullableIntToInt() { int?i = 10; int r = TypeCast.ChangeToTypeOrNullableType <int>(i); Assert.IsTrue(r == 10); }
public void TestConvertNullToNullableInt() { object o = null; int? r = TypeCast.ChangeToTypeOrNullableType <int?>(o); Assert.IsTrue(!r.HasValue); }
public void TestConvertToNullableInt() { object o = 3; int? r = TypeCast.ChangeToTypeOrNullableType <int?>(o); Assert.AreEqual(3, r); }
private bool TryConvertOneArgument(object val, Type targetType, out object result) { //Extend:try improve performance here try { result = TypeCast.ChangeToTypeOrNullableType(val, targetType); return(true); } catch { result = null; return(false); } }
internal static object ConvertToSimpleType(string jsonText, Type targetType, bool ignoreTypeSafe = false) { IExtendConverter cvt = ExtendConverter.Instance(); object result = null; if (cvt.CanConvert(targetType)) { try { result = cvt.FromJson(targetType, jsonText, ignoreTypeSafe); } catch (Exception ex) { throw ex.CreateWrapException <JsonExtendConverterException>(); } } else { if (ignoreTypeSafe) { //Note: If we don't care type safe, always remove quoter if exist, then cast if (jsonText.StartsWith(JsonConstant.Quot) && jsonText.EndsWith(JsonConstant.Quot) && jsonText.Length > 1) { jsonText = jsonText.UnBracketing(StringPair.DoubleQuote); } result = TypeCast.ChangeToTypeOrNullableType(jsonText, targetType); } else { //Note: If we care type, strict follow JSON standard if (targetType == typeof(string)) { if ((!jsonText.StartsWith(JsonConstant.Quot)) || (!jsonText.EndsWith(JsonConstant.Quot))) { ExceptionHelper.ThrowSyntaxNoQuotError(); } result = jsonText.UnBracketing(StringPair.DoubleQuote); } else { result = TypeCast.ChangeToTypeOrNullableType(jsonText, targetType); } } } return(result); }
internal static T MapToEntity <T>(this DataRow dr, IObjectMapInfo map, string alias = null) { if (IsNull(dr, alias, map)) { return(default(T)); } T entity = Activator.CreateInstance <T>(); IReflector r = Reflector.Bind(entity); foreach (var pmi in map.PropertyMaps) { object val = dr[alias + pmi.ColumnName]; Type propertyType = r.GetPropertyType(pmi.PropertyName); if (propertyType.IsEnum) { if (val is string) { r.SetPropertyValue(pmi.PropertyName, EnumEx.Parse(propertyType, val as string, false)); } else { r.SetPropertyValue(pmi.PropertyName, EnumEx.Parse(propertyType, (int)System.Convert.ChangeType(val, typeof(int)))); } } else { if (val is System.DBNull) { r.SetPropertyValue(pmi.PropertyName, null); } else { r.SetPropertyValue(pmi.PropertyName, TypeCast.ChangeToTypeOrNullableType(val, propertyType)); } } } return(entity); }
private static void CopyByBoth(IDictionary <string, string> src, object dest) { IReflector r = Reflector.Bind(dest); foreach (var key in src.Keys) { if (r.ExistProperty(key)) { try { Type pType = r.GetPropertyType(key); object val = TypeCast.ChangeToTypeOrNullableType(src[key], pType); r.SetPropertyValue(key, val); } catch (Exception ex) { throw ex.CreateWrapException <CopyException>(); } } } }
public T Create <T>(T entity) { try { string sql = _emit.Create(entity); _log.Debug(sql); _exe.ExecuteNonQuery(sql); var pk = _emit.GetPrimaryKeyNeedBinding <T>(); if (pk != null) { string sqlId = _emit.FindIdentity <T>(); _log.Debug(sqlId); var id = _exe.ExecuteScalar(sqlId); IReflector r = Reflector.Bind(entity); r.SetPropertyValue(pk.PropertyName, TypeCast.ChangeToTypeOrNullableType(id, r.GetPropertyType(pk.PropertyName))); } return(entity); } catch (Exception ex) { throw ex.CreateWrapException <ORMException>(); } }
public T Eval <T>(string expr) { object val = this.Eval(expr); return(TypeCast.ChangeToTypeOrNullableType <T>(val)); }