private static bool HasJsonConstructorAttribute(ConstructorInfo constructorInfo)
=> constructorInfo.GetCustomAttribute<JsonConstructorAttribute>() != null;
- public static bool HasRequiredMemberAttribute(this ICustomAttributeProvider memberInfo)
+ public static bool HasRequiredMemberAttribute(this MemberInfo memberInfo)
{
// For compiler related attributes we should only look at full type name rather than trying to do something different for version when attribute was introduced.
// I.e. library is targeting netstandard2.0 with polyfilled attributes and is being consumed by an app targeting net7.0 or greater.
- return memberInfo.HasCustomAttributeWithName("System.Runtime.CompilerServices.RequiredMemberAttribute", inherit: true);
+ return memberInfo.HasCustomAttributeWithName("System.Runtime.CompilerServices.RequiredMemberAttribute", inherit: false);
}
- public static bool HasSetsRequiredMembersAttribute(this ICustomAttributeProvider memberInfo)
+ public static bool HasSetsRequiredMembersAttribute(this MemberInfo memberInfo)
{
// See comment for HasRequiredMemberAttribute for why we need to always only look at full name
- return memberInfo.HasCustomAttributeWithName("System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute", inherit: true);
+ return memberInfo.HasCustomAttributeWithName("System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute", inherit: false);
}
- private static bool HasCustomAttributeWithName(this ICustomAttributeProvider memberInfo, string fullName, bool inherit)
+ private static bool HasCustomAttributeWithName(this MemberInfo memberInfo, string fullName, bool inherit)
{
foreach (object attribute in memberInfo.GetCustomAttributes(inherit))
{
// Walk the type hierarchy starting from the current type up to the base type(s)
foreach (Type currentType in typeInfo.Type.GetSortedTypeHierarchy())
{
- if (currentType == JsonTypeInfo.ObjectType)
+ if (currentType == JsonTypeInfo.ObjectType ||
+ currentType == typeof(ValueType))
{
- // Don't process any members for typeof(object)
+ // Don't process any members for typeof(object) or System.ValueType
break;
}
- // Compiler adds RequiredMemberAttribute to type if any of the members are marked with 'required' keyword.
- bool shouldCheckMembersForRequiredMemberAttribute =
- !constructorHasSetsRequiredMembersAttribute && currentType.HasRequiredMemberAttribute();
-
AddMembersDeclaredBySuperType(
typeInfo,
currentType,
- shouldCheckMembersForRequiredMemberAttribute,
+ constructorHasSetsRequiredMembersAttribute,
ref state);
}
private static void AddMembersDeclaredBySuperType(
JsonTypeInfo typeInfo,
Type currentType,
- bool shouldCheckMembersForRequiredMemberAttribute,
+ bool constructorHasSetsRequiredMembersAttribute,
ref JsonTypeInfo.PropertyHierarchyResolutionState state)
{
Debug.Assert(!typeInfo.IsReadOnly);
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly;
+ // Compiler adds RequiredMemberAttribute to type if any of the members are marked with 'required' keyword.
+ bool shouldCheckMembersForRequiredMemberAttribute =
+ !constructorHasSetsRequiredMembersAttribute && currentType.HasRequiredMemberAttribute();
+
foreach (PropertyInfo propertyInfo in currentType.GetProperties(BindingFlags))
{
// Ignore indexers and virtual properties that have overrides that were [JsonIgnore]d.