From f317f45d141969f0fcb3163702bdc534767b6287 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 16 May 2019 12:03:36 -0400 Subject: [PATCH] type change fix --- src/App/Services/PreferencesStorageService.cs | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/App/Services/PreferencesStorageService.cs b/src/App/Services/PreferencesStorageService.cs index f458cbdb8..f7a54c3a3 100644 --- a/src/App/Services/PreferencesStorageService.cs +++ b/src/App/Services/PreferencesStorageService.cs @@ -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(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(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(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(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(val)); } else { @@ -116,5 +116,19 @@ namespace Bit.App.Services } return Task.FromResult(0); } + + private static T ChangeType(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); + } } }