1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-12-17 15:27:43 +01:00

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?))
{
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?))
{
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?))
{
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?))
{
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?))
{
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
{
@ -116,5 +116,19 @@ namespace Bit.App.Services
}
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);
}
}
}