diff --git a/Tomlet/TomlCompositeDeserializer.cs b/Tomlet/TomlCompositeDeserializer.cs index 5a6b138..4431174 100644 --- a/Tomlet/TomlCompositeDeserializer.cs +++ b/Tomlet/TomlCompositeDeserializer.cs @@ -35,12 +35,17 @@ public static TomlSerializationMethods.Deserialize For(Type type, TomlSe else { //Get all instance fields - var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var memberFlags = BindingFlags.Public | BindingFlags.Instance; + if (!options.IgnoreNonPublicMembers) { + memberFlags |= BindingFlags.NonPublic; + } + + var fields = type.GetFields(memberFlags); //Ignore NonSerialized and CompilerGenerated fields. fields = fields.Where(f => !f.IsNotSerialized && GenericExtensions.GetCustomAttribute(f) == null).ToArray(); - var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var props = type.GetProperties(memberFlags); //Ignore TomlNonSerializedAttribute Decorated Properties var propsDict = props diff --git a/Tomlet/TomlCompositeSerializer.cs b/Tomlet/TomlCompositeSerializer.cs index e73df1d..bcb034b 100644 --- a/Tomlet/TomlCompositeSerializer.cs +++ b/Tomlet/TomlCompositeSerializer.cs @@ -22,10 +22,15 @@ public static TomlSerializationMethods.Serialize For(Type type, TomlSeri else { //Get all instance fields - var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var memberFlags = BindingFlags.Public | BindingFlags.Instance; + if (!options.IgnoreNonPublicMembers) { + memberFlags |= BindingFlags.NonPublic; + } + + var fields = type.GetFields(memberFlags); var fieldAttribs = fields .ToDictionary(f => f, f => new {inline = GenericExtensions.GetCustomAttribute(f), preceding = GenericExtensions.GetCustomAttribute(f), noInline = GenericExtensions.GetCustomAttribute(f)}); - var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) + var props = type.GetProperties(memberFlags) .ToArray(); var propAttribs = props .ToDictionary(p => p, p => new {inline = GenericExtensions.GetCustomAttribute(p), preceding = GenericExtensions.GetCustomAttribute(p), prop = GenericExtensions.GetCustomAttribute(p), noInline = GenericExtensions.GetCustomAttribute(p)}); diff --git a/Tomlet/TomlSerializerOptions.cs b/Tomlet/TomlSerializerOptions.cs index 5ced93b..8a8c0f2 100644 --- a/Tomlet/TomlSerializerOptions.cs +++ b/Tomlet/TomlSerializerOptions.cs @@ -4,5 +4,6 @@ public class TomlSerializerOptions { public static TomlSerializerOptions Default = new(); public bool OverrideConstructorValues { get; set; } = false; + public bool IgnoreNonPublicMembers { get; set; } = false; } } \ No newline at end of file