public static object ChangeType(object item, Type destinationType) { if (item == null || item == DBNull.Value) { return destinationType == typeof(DBNull) ? DBNull.Value : null; } var itemType = item.GetType(); if (itemType == typeof(string)) { object result; if (TryStringToEnum(item, destinationType, out result)) { return result; } } else if (itemType.IsClass) { return ReflectionCast(item, destinationType); } var tmpItem = new CastItem(item, itemType); tmpItem = TryUnpackNullable(tmpItem); tmpItem = TryUnpackEnum(tmpItem); item = tmpItem.Value; return Pack(item, destinationType); }
private static CastItem TryUnpackNullable(CastItem item) { var type = Nullable.GetUnderlyingType(item.Type); if (type == null) return item; var value = Convert.ChangeType(item.Value, type); return new CastItem(value, type); }
private static CastItem TryUnpackEnum(CastItem item) { if (!item.Type.IsEnum) return item; var type = Enum.GetUnderlyingType(item.Type); var value = Convert.ChangeType(item.Value, type); return new CastItem(value, type); }
public CastVm(CastItem castItem, IAppContext appContext) : base(castItem.thumbnail, appContext) { _castItem = castItem; _appContext = appContext; Name = _castItem.name; Role = _castItem.role; }
private CastVm ItemFactory(CastItem arg) { if (first) { first = false; return(new FirstCastVm(arg, _server)); } return(new CastVm(arg, _server)); }
public FirstCastVm(CastItem castItem, AppContext appContext) : base(castItem, appContext) { ColSpan = 2; RowSpan = 2; }