type change fix

This commit is contained in:
Kyle Spearrin 2019-05-16 12:03:36 -04:00
parent 5f6a3f4cb5
commit f317f45d14

View file

@ -37,27 +37,27 @@ namespace Bit.App.Services
else if(objType == typeof(bool) || objType == typeof(bool?)) else if(objType == typeof(bool) || objType == typeof(bool?))
{ {
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(bool), _sharedName); var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(bool), _sharedName);
return Task.FromResult((T)Convert.ChangeType(val, objType)); return Task.FromResult(ChangeType<T>(val));
} }
else if(objType == typeof(int) || objType == typeof(int?)) else if(objType == typeof(int) || objType == typeof(int?))
{ {
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(int), _sharedName); var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(int), _sharedName);
return Task.FromResult((T)Convert.ChangeType(val, objType)); return Task.FromResult(ChangeType<T>(val));
} }
else if(objType == typeof(long) || objType == typeof(long?)) else if(objType == typeof(long) || objType == typeof(long?))
{ {
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(long), _sharedName); var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(long), _sharedName);
return Task.FromResult((T)Convert.ChangeType(val, objType)); return Task.FromResult(ChangeType<T>(val));
} }
else if(objType == typeof(double) || objType == typeof(double?)) else if(objType == typeof(double) || objType == typeof(double?))
{ {
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(double), _sharedName); var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(double), _sharedName);
return Task.FromResult((T)Convert.ChangeType(val, objType)); return Task.FromResult(ChangeType<T>(val));
} }
else if(objType == typeof(DateTime) || objType == typeof(DateTime?)) else if(objType == typeof(DateTime) || objType == typeof(DateTime?))
{ {
var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(DateTime), _sharedName); var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(DateTime), _sharedName);
return Task.FromResult((T)Convert.ChangeType(val, objType)); return Task.FromResult(ChangeType<T>(val));
} }
else else
{ {
@ -116,5 +116,19 @@ namespace Bit.App.Services
} }
return Task.FromResult(0); return Task.FromResult(0);
} }
private static T ChangeType<T>(object value)
{
var t = typeof(T);
if(t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if(value == null)
{
return default(T);
}
t = Nullable.GetUnderlyingType(t);
}
return (T)Convert.ChangeType(value, t);
}
} }
} }