Skip to content

Commit

Permalink
Add workaround for netstandard1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
pensono committed Apr 19, 2020
1 parent ac7664b commit 4ba7eb6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 25 additions & 0 deletions YamlDotNet/Helpers/Portability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TAttribute>(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<Attribute>();
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
Expand Down Expand Up @@ -374,6 +393,12 @@ public static bool IsInstanceOf(this Type type, object o)
{
return type.IsInstanceOfType(o);
}

public static Attribute[] GetAllCustomAttributes<TAttribute>(this PropertyInfo property)
{
// Don't use IMemberInfo.GetCustomAttributes, it ignores the inherit parameter
return Attribute.GetCustomAttributes(property, typeof(TAttribute));
}
}

internal sealed class CultureInfoAdapter : CultureInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public void Write(object target, object? value)

public T GetCustomAttribute<T>() where T : Attribute
{
// Don't use IMemberInfo.GetCustomAttributes, it ignores the inherit parameter
var attributes = Attribute.GetCustomAttributes(propertyInfo, typeof(T));
var attributes = propertyInfo.GetAllCustomAttributes<T>();
return (T)attributes.FirstOrDefault();
}

Expand Down

0 comments on commit 4ba7eb6

Please sign in to comment.