From 1aa6a409821e8aef01266863272ddcad3c5e7c28 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Mon, 9 Oct 2023 17:58:20 +0200 Subject: [PATCH] Correctly handle inheritance of EntrySerializeAttributes --- .../DocumentManager.cs | 17 -------- .../EntryConvert/EntryConvert.cs | 6 --- .../EntrySerializeSerialization.cs | 39 ++++++++++++------- .../Serialization/EntrySerializeDummies.cs | 5 +-- 4 files changed, 25 insertions(+), 42 deletions(-) delete mode 100644 src/Moryx.Resources.Samples/DocumentManager.cs diff --git a/src/Moryx.Resources.Samples/DocumentManager.cs b/src/Moryx.Resources.Samples/DocumentManager.cs deleted file mode 100644 index b9e838fd2..000000000 --- a/src/Moryx.Resources.Samples/DocumentManager.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Moryx.AbstractionLayer.Resources; -using Moryx.Serialization; -using System.Collections.Generic; -using System.ComponentModel; -using System.Runtime.Serialization; - -namespace Moryx.Resources.Demo -{ - [ResourceRegistration] - [EntrySerialize] - public class DocumentManager : PublicResource - { - - [EntrySerialize] - public string FolderPath { get; set; } - } -} diff --git a/src/Moryx/Serialization/EntryConvert/EntryConvert.cs b/src/Moryx/Serialization/EntryConvert/EntryConvert.cs index d01790c8f..b62c19f28 100644 --- a/src/Moryx/Serialization/EntryConvert/EntryConvert.cs +++ b/src/Moryx/Serialization/EntryConvert/EntryConvert.cs @@ -275,12 +275,6 @@ public static Entry EncodeObject(object instance, ICustomSerialization customSer var propertyType = property.PropertyType; if (propertyType == instanceType) continue; - if(propertyType == typeof(Object)) - { - var propertyValue = property.GetValue(instance); - if (propertyValue != null && propertyValue?.GetType() == instanceType) - continue; - } var convertedProperty = EncodeProperty(property, customSerialization); diff --git a/src/Moryx/Serialization/EntrySerializeSerialization.cs b/src/Moryx/Serialization/EntrySerializeSerialization.cs index 4c4f01b3a..f2e347204 100644 --- a/src/Moryx/Serialization/EntrySerializeSerialization.cs +++ b/src/Moryx/Serialization/EntrySerializeSerialization.cs @@ -42,9 +42,9 @@ public EntrySerializeSerialization(Type filterBaseType) public override IEnumerable GetConstructors(Type sourceType) { var constructors = from ctor in base.GetConstructors(sourceType) - let mode = EvaluateSerializeMode(ctor) - where mode.HasValue && mode.Value == EntrySerializeMode.Always - select ctor; + let mode = EvaluateSerializeMode(ctor) + where mode.HasValue && mode.Value == EntrySerializeMode.Always + select ctor; return constructors; } @@ -54,9 +54,9 @@ public override IEnumerable GetMethods(Type sourceType) { var methods = sourceType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(m => !m.IsSpecialName); methods = from method in methods - let mode = EvaluateSerializeMode(method) - where mode.HasValue && mode.Value == EntrySerializeMode.Always - select method; + let mode = EvaluateSerializeMode(method) + where mode.HasValue && mode.Value == EntrySerializeMode.Always + select method; return methods; } @@ -66,14 +66,6 @@ public override IEnumerable GetProperties(Type sourceType) { var properties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(BasePropertyFilter).Where(ExplicitPropertyFilter).ToArray(); - var attributeOnClass = sourceType.GetCustomAttribute(false); - var attributeOnClassOrBaseClass = sourceType.GetCustomAttribute(true); - // to-do: remove this in .net8 and use restriction on the EntrySerializeAttribute - //EntrySerialize property on both class and base class - if (attributeOnClass != null && attributeOnClassOrBaseClass != null) - { - properties = properties.Where(property => property.GetCustomAttribute() != null).ToArray(); - } return EntrySerializeAttributeFilter(sourceType, properties); } @@ -154,6 +146,23 @@ private static IEnumerable EntrySerializeAttributeFilter(Type sour return properties; } + /// + /// Iterate the inheritance tree and find lowest declaration of the attribute + /// + private static EntrySerializeMode? EvaluateSerializeMode(Type attributeProvider) + { + // If more than 1 is declared, determine the lowest definition as it takes precedence + // For each declaration check assignability to determine lower type + var currentType = attributeProvider; + EntrySerializeAttribute lowestDeclaration = null; + while (currentType != typeof(object)) + { + lowestDeclaration = currentType.GetCustomAttribute(false) ?? lowestDeclaration; + currentType = currentType.BaseType; + } + return lowestDeclaration?.Mode; + } + /// /// Checks if the is existent and activated /// @@ -168,7 +177,7 @@ private static PropertyMode EvaluateSerializeMode(PropertyInfo property) return new PropertyMode { Property = property, - Mode = EvaluateSerializeMode((ICustomAttributeProvider)property) + Mode = property.GetCustomAttribute(true)?.Mode }; } diff --git a/src/Tests/Moryx.Tests/Serialization/EntrySerializeDummies.cs b/src/Tests/Moryx.Tests/Serialization/EntrySerializeDummies.cs index 40625274b..2a69691bb 100644 --- a/src/Tests/Moryx.Tests/Serialization/EntrySerializeDummies.cs +++ b/src/Tests/Moryx.Tests/Serialization/EntrySerializeDummies.cs @@ -82,13 +82,10 @@ public class EntrySerialize_AlwaysClassAlwaysMember public string AlwaysProperty { get; set; } = "123456"; [EntrySerialize] public int Property1 { get; set; } - [EntrySerialize] - public int Property2 { get; set; } - - public object Property => this; [EntrySerialize] public EntrySerialize_AlwaysClassAlwaysMember AnotherProperty { get; set; } + internal IExplicitInterface ExplicitInterface { get; } }