From 4ba7eb6ef59530138f06ed858b803e3fe6b15d79 Mon Sep 17 00:00:00 2001 From: Ethan Shea Date: Sun, 19 Apr 2020 15:38:41 -0700 Subject: [PATCH] Add workaround for netstandard1.3 --- YamlDotNet/Helpers/Portability.cs | 25 +++++++++++++++++++ .../ReadablePropertiesTypeInspector.cs | 3 +-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/YamlDotNet/Helpers/Portability.cs b/YamlDotNet/Helpers/Portability.cs index 329cf74cc..eccba1743 100644 --- a/YamlDotNet/Helpers/Portability.cs +++ b/YamlDotNet/Helpers/Portability.cs @@ -249,6 +249,25 @@ public static bool IsInstanceOf(this Type type, object o) { return o.GetType() == type || o.GetType().GetTypeInfo().IsSubclassOf(type); } + + public static Attribute[] GetAllCustomAttributes(this PropertyInfo member) + { + // IMemberInfo.GetCustomAttributes ignores it's "inherit" parameter for properties, + // and the suggested replacement (Attribute.GetCustomAttributes) is not available + // on netstandard1.3 + var result = new List(); + var type = member.DeclaringType; + + while (type != null) + { + type.GetPublicProperty(member.Name); + result.AddRange(member.GetCustomAttributes(typeof(TAttribute))); + + type = type.BaseType(); + } + + return result.ToArray(); + } } internal sealed class CultureInfoAdapter : CultureInfo @@ -374,6 +393,12 @@ public static bool IsInstanceOf(this Type type, object o) { return type.IsInstanceOfType(o); } + + public static Attribute[] GetAllCustomAttributes(this PropertyInfo property) + { + // Don't use IMemberInfo.GetCustomAttributes, it ignores the inherit parameter + return Attribute.GetCustomAttributes(property, typeof(TAttribute)); + } } internal sealed class CultureInfoAdapter : CultureInfo diff --git a/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs b/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs index 0712c98e0..da8cfb9cc 100644 --- a/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs +++ b/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs @@ -79,8 +79,7 @@ public void Write(object target, object? value) public T GetCustomAttribute() where T : Attribute { - // Don't use IMemberInfo.GetCustomAttributes, it ignores the inherit parameter - var attributes = Attribute.GetCustomAttributes(propertyInfo, typeof(T)); + var attributes = propertyInfo.GetAllCustomAttributes(); return (T)attributes.FirstOrDefault(); }