From 9a2b8a32eb9cd0532550d6c93a5620a87bcc84d5 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 22 Oct 2023 20:19:37 +0200 Subject: [PATCH 1/2] Update MauiSettingsObjectHelper.cs --- .../Helper/MauiSettingsObjectHelper.cs | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/MauiSettings/Helper/MauiSettingsObjectHelper.cs b/src/MauiSettings/Helper/MauiSettingsObjectHelper.cs index 2a66079..649b1dd 100644 --- a/src/MauiSettings/Helper/MauiSettingsObjectHelper.cs +++ b/src/MauiSettings/Helper/MauiSettingsObjectHelper.cs @@ -62,23 +62,30 @@ public static void SetSettingValue(MemberInfo memberInfo, object settings, objec } catch (Exception exc) { - + throw new NotSupportedException($"MauiSettings: The type '{memberInfo.GetType()}' is not supported for the field: {memberInfo.Name}", exc); } - throw new NotSupportedException($"MauiSettings: The type '{memberInfo.GetType()}' is not supported for the field: {memberInfo.Name}"); + } public static Type GetSettingType(MemberInfo memberInfo) { - if (memberInfo is FieldInfo fieldInfo) + try { - return fieldInfo.FieldType; + if (memberInfo is FieldInfo fieldInfo) + { + return fieldInfo.FieldType; + } + if (memberInfo is PropertyInfo propertyInfo) + { + return propertyInfo.PropertyType; + } + else + return default; } - - if (memberInfo is PropertyInfo propertyInfo) + catch (Exception exc) { - return propertyInfo.PropertyType; + throw new NotSupportedException($"MauiSettings: The type '{memberInfo.GetType()}' is not supported for the field: {memberInfo.Name}", exc); } - throw new NotSupportedException($"MauiSettings: The type '{memberInfo.GetType()}' is not supported for the field: {memberInfo.Name}"); } public static object GetTypeDefaultValue(Type type) @@ -107,10 +114,9 @@ public static object GetDefaultValue(MauiSettingBaseAttribute attr, Type setting } return obj; } - return GetTypeDefaultValue(settingType); } - catch (Exception exc) + catch (Exception) { return GetTypeDefaultValue(settingType); } @@ -124,7 +130,6 @@ public static object GetConvertedTypeValue(object setting, Type settingsType) { if (setting?.GetType() == settingsType) return setting; - // return setting.GetType() == typeof(string) ? Activator.CreateInstance(settingsType, new string[] { setting as string }) : Convert.ChangeType(setting, settingsType); From 497b8ef1f86a6a34312bf7c1cb82f2fb60d492db Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 23 Oct 2023 12:10:40 +0200 Subject: [PATCH 2/2] Update MauiSettingsHelper.cs Fixed issue on datatype change --- src/MauiSettings/Helper/MauiSettingsHelper.cs | 80 ++++++++++--------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/src/MauiSettings/Helper/MauiSettingsHelper.cs b/src/MauiSettings/Helper/MauiSettingsHelper.cs index f696074..4a42e63 100644 --- a/src/MauiSettings/Helper/MauiSettingsHelper.cs +++ b/src/MauiSettings/Helper/MauiSettingsHelper.cs @@ -20,43 +20,51 @@ internal class MauiSettingsHelper public static T GetSettingsValue(string key, T defaultValue) { object returnValue = null; - switch (defaultValue) + try { - case bool b: - returnValue = Preferences.Get(key, b); - break; - case double d: - returnValue = Preferences.Get(key, d); - break; - case int i: - returnValue = Preferences.Get(key, i); - break; - case float f: - returnValue = Preferences.Get(key, f); - break; - case long l: - returnValue = Preferences.Get(key, l); - break; - case string s: - returnValue = Preferences.Get(key, s); - break; - case DateTime dt: - returnValue = Preferences.Get(key, dt); - break; - default: - // For all other types try to serialize it as JSON - string jsonString = Preferences.Get(key, string.Empty) ?? string.Empty; - if (defaultValue == null) - { - // In this case it's unkown to what data type the string should be deserialized. - // So just return the string as it is to avoid exceptions while converting. - returnValue = jsonString; - } - else - { - returnValue = JsonConvert.DeserializeObject(jsonString); - } - break; + switch (defaultValue) + { + case bool b: + returnValue = Preferences.Get(key, b); + break; + case double d: + returnValue = Preferences.Get(key, d); + break; + case int i: + returnValue = Preferences.Get(key, i); + break; + case float f: + returnValue = Preferences.Get(key, f); + break; + case long l: + returnValue = Preferences.Get(key, l); + break; + case string s: + returnValue = Preferences.Get(key, s); + break; + case DateTime dt: + returnValue = Preferences.Get(key, dt); + break; + default: + // For all other types try to serialize it as JSON + string jsonString = Preferences.Get(key, string.Empty) ?? string.Empty; + if (defaultValue == null) + { + // In this case it's unkown to what data type the string should be deserialized. + // So just return the string as it is to avoid exceptions while converting. + returnValue = jsonString; + } + else + { + returnValue = JsonConvert.DeserializeObject(jsonString); + } + break; + } + } + catch (Exception ex) + { + SetSettingsValue(key, defaultValue); + return defaultValue; } return ChangeSettingsType(returnValue, defaultValue); //return (T)Convert.ChangeType(returnValue, typeof(T));