Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Int32 is persisted as Int64 #49

Open
sstainba opened this issue Aug 29, 2023 · 1 comment
Open

Int32 is persisted as Int64 #49

sstainba opened this issue Aug 29, 2023 · 1 comment

Comments

@sstainba
Copy link

sstainba commented Aug 29, 2023

I have several int properties I am storing, but when I get them back out, they are returned as Int64.

int myInt = 12;
localSettings.Store("myInt", myInt);

myInt = localSettings.Get("myInt");  //this fails as the value returned is an Int64

The save method for my settings...

public void Save(string propertyName)
{
    if(!isLoaded) return;
    var prop = this.GetType().GetProperty(propertyName);
    if (prop == null) return;
    var val = prop.GetValue(this);
    localSettings.Store(propertyName, val);
}

The load method...

public Settings() {
     isLoaded = false;
     localSettings.Load();
     var props = typeof(Settings).GetProperties().Where(p => p.CanWrite);

     foreach (var k in localSettings.Keys())
     {
         var p = props.FirstOrDefault(x => x.Name == k);
         if (p == null) continue;

         var val = localSettings.Get(k);

         p.SetValue(this, localSettings.Get(k));
     }
     isLoaded = true;
 }
@Rolschau
Copy link

Hi @sstainba

It is a known issue with the deserialization where it does not know if the value is Int32 or Int64 so defaults to Int64 just-in-case. See the longer explanation at https://stackoverflow.com/a/44010307

Try changing the type of what is returned to the same type as the property.

p.SetValue(this, Convert.ChangeType(val, p.PropertyType));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants