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

Ignore Public Static Properties #22

Open
bluebat-swiss opened this issue Feb 2, 2022 · 0 comments
Open

Ignore Public Static Properties #22

bluebat-swiss opened this issue Feb 2, 2022 · 0 comments

Comments

@bluebat-swiss
Copy link

Hi

There is a problem with TypeExtensions.GetPublicInstanceProperties(). it get's also all static and nonpublic properties.

A short blink into ILSpy I saw that GetRuntimeProperties() will call GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic). So GetPublicInstanceProperties() will also get static and nonpublic properties.

I changed the code to solve this problem:

  1. extended AdvancedSharpSerializerBinarySettings with two new properties
    public bool IncludeStaticProperties { get; set; }
    public bool IncludeNonPublicProperties { get; set; }

  2. extended the PropertyProvider with two new properties
    public bool IncludeStatic { get; set; }
    public bool IncludeNonPublic { get; set; }

  3. assigning the props in SharpSerializer.initialize()
    PropertyProvider.IncludeNonPublic = settings.AdvancedSettings.IncludeNonPublicProperties;
    PropertyProvider.IncludeStatic = settings.AdvancedSettings.IncludeStaticProperties;

  4. finally changed PropertyProvider.GetAllProperties()

protected virtual PropertyInfo[] GetAllProperties(Type type)
{
  var flags = BindingFlags.Instance | BindingFlags.Public;
  
  if (IncludeStatic)
    flags |= BindingFlags.Static;
  
  if (IncludeNonPublic)
    flags |= BindingFlags.NonPublic;
  
  return type.GetProperties(flags);
}

Hope, this helps someone with the same problem.
Cheers, jaz (bluebat)

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

1 participant