SettingsPlugin: Calling AddOrUpdateValue with a string throws "Value of type String is not supported"

Calling AddOrUpdateValue with string type throws exception Value of type String is not supported. Here’s how this is possible:

object strValue = "a";

CrossSettings.Current.AddOrUpdateValue(key, strValue);

This is because the implementation is using Type.GetTypeCode to get the primitive type, but this returns System.TypeCode.Object, and not System.TypeCode.String as you could expect.

https://github.com/jamesmontemagno/SettingsPlugin/blob/master/src/Plugin.Settings.Android/Settings.cs#L207

Same issue is on iOS, it’s very similar code.

As a solution, maybe use Convert.GetTypeCode which correctly returns TypeCode.String?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 23 (6 by maintainers)

Commits related to this issue

Most upvoted comments

@opcodewriter I would simply do something like this as the non-generic version:

public bool AddOrUpdateValue(string key, object value)
{
  if(value == null)
  {
    throw new ArgumentNullException(nameof(value));
  }
   return AddOrUpdateValue(key, value, Type.GetTypeCode(value.GetType()));
}

The already existing private AddOrUpdateValue is unchanged and handles all the cases, including the typecode specific ones. I don’t have any opinion about complex types, as they are not part of the current code.

Yes, i should pass typecode 😃 which i already have, will fix that.

You can’t pass it an object like that, you have to make it:

string strValue = “a”;