From ae5737eccef97eea01211599a5ef87cf89c99842 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 24 Aug 2021 16:43:22 -0600 Subject: [PATCH] Fix nullable annotations for AssociatedMetadataTypeTypeDescriptionProvider (#57979) Co-authored-by: Stephen Toub --- .../ref/System.ComponentModel.Annotations.cs | 5 +---- ...ssociatedMetadataTypeTypeDescriptionProvider.cs | 9 +++------ .../AssociatedMetadataTypeTypeDescriptor.cs | 23 ++++++++++------------ 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/libraries/System.ComponentModel.Annotations/ref/System.ComponentModel.Annotations.cs b/src/libraries/System.ComponentModel.Annotations/ref/System.ComponentModel.Annotations.cs index aa1912c..c78333d 100644 --- a/src/libraries/System.ComponentModel.Annotations/ref/System.ComponentModel.Annotations.cs +++ b/src/libraries/System.ComponentModel.Annotations/ref/System.ComponentModel.Annotations.cs @@ -6,15 +6,12 @@ namespace System.ComponentModel.DataAnnotations { - // TODO-NULLABLE: Enable after System.ComponentModel.TypeDescriptionProvider is annotated -#nullable disable public partial class AssociatedMetadataTypeTypeDescriptionProvider : System.ComponentModel.TypeDescriptionProvider { public AssociatedMetadataTypeTypeDescriptionProvider(System.Type type) { } public AssociatedMetadataTypeTypeDescriptionProvider(System.Type type, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type associatedMetadataType) { } - public override System.ComponentModel.ICustomTypeDescriptor GetTypeDescriptor([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type objectType, object instance) { throw null; } + public override System.ComponentModel.ICustomTypeDescriptor GetTypeDescriptor([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type objectType, object? instance) { throw null; } } -#nullable enable [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false, Inherited=true)] [System.ObsoleteAttribute("AssociationAttribute has been deprecated and is not supported.")] public sealed partial class AssociationAttribute : System.Attribute diff --git a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptionProvider.cs b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptionProvider.cs index a408e38..fbfe36a 100644 --- a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptionProvider.cs +++ b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptionProvider.cs @@ -1,9 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// TODO-NULLABLE: Enable after System.ComponentModel.TypeDescriptionProvider is annotated -#nullable disable - using System.Diagnostics.CodeAnalysis; namespace System.ComponentModel.DataAnnotations @@ -15,7 +12,7 @@ namespace System.ComponentModel.DataAnnotations public class AssociatedMetadataTypeTypeDescriptionProvider : TypeDescriptionProvider { [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - private readonly Type _associatedMetadataType; + private readonly Type? _associatedMetadataType; /// /// Initializes a new instance of the System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider @@ -53,9 +50,9 @@ namespace System.ComponentModel.DataAnnotations /// The type of object to retrieve the type descriptor for. /// An instance of the type. /// The descriptor that provides metadata for the type. - public override ICustomTypeDescriptor GetTypeDescriptor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType, object instance) + public override ICustomTypeDescriptor GetTypeDescriptor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType, object? instance) { - ICustomTypeDescriptor baseDescriptor = base.GetTypeDescriptor(objectType, instance); + ICustomTypeDescriptor? baseDescriptor = base.GetTypeDescriptor(objectType, instance); return new AssociatedMetadataTypeTypeDescriptor(baseDescriptor, objectType, _associatedMetadataType); } } diff --git a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptor.cs b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptor.cs index 5c49bd6..ab8b86b 100644 --- a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptor.cs +++ b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptor.cs @@ -1,9 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// TODO-NULLABLE: Enable after System.ComponentModel.TypeDescriptionProvider is annotated -#nullable disable - using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -15,14 +12,14 @@ namespace System.ComponentModel.DataAnnotations internal sealed class AssociatedMetadataTypeTypeDescriptor : CustomTypeDescriptor { [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - private Type AssociatedMetadataType { get; set; } + private Type? AssociatedMetadataType { get; set; } private bool IsSelfAssociated { get; set; } public AssociatedMetadataTypeTypeDescriptor( - ICustomTypeDescriptor parent, + ICustomTypeDescriptor? parent, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type type, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type associatedMetadataType) + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? associatedMetadataType) : base(parent) { AssociatedMetadataType = associatedMetadataType ?? TypeDescriptorCache.GetAssociatedMetadataType(type); @@ -34,7 +31,7 @@ namespace System.ComponentModel.DataAnnotations } [RequiresUnreferencedCode("PropertyDescriptor's PropertyType cannot be statically discovered. The public parameterless constructor or the 'Default' static field may be trimmed from the Attribute's Type.")] - public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) + public override PropertyDescriptorCollection GetProperties(Attribute[]? attributes) { return GetPropertiesWithMetadata(base.GetProperties(attributes)); } @@ -96,7 +93,7 @@ namespace System.ComponentModel.DataAnnotations private static class TypeDescriptorCache { // Stores the associated metadata type for a type - private static readonly ConcurrentDictionary s_metadataTypeCache = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary s_metadataTypeCache = new ConcurrentDictionary(); // Stores the attributes for a member info private static readonly ConcurrentDictionary<(Type, string), Attribute[]> s_typeMemberCache = new ConcurrentDictionary<(Type, string), Attribute[]>(); @@ -117,16 +114,16 @@ namespace System.ComponentModel.DataAnnotations } [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - public static Type GetAssociatedMetadataType(Type type) + public static Type? GetAssociatedMetadataType(Type type) { - Type associatedMetadataType = null; + Type? associatedMetadataType; if (s_metadataTypeCache.TryGetValue(type, out associatedMetadataType)) { return associatedMetadataType; } // Try association attribute - MetadataTypeAttribute attribute = (MetadataTypeAttribute)Attribute.GetCustomAttribute(type, typeof(MetadataTypeAttribute)); + MetadataTypeAttribute? attribute = (MetadataTypeAttribute?)Attribute.GetCustomAttribute(type, typeof(MetadataTypeAttribute)); if (attribute != null) { associatedMetadataType = attribute.MetadataClassType; @@ -164,7 +161,7 @@ namespace System.ComponentModel.DataAnnotations string memberName) { (Type, string) memberTuple = (type, memberName); - Attribute[] attributes; + Attribute[]? attributes; if (s_typeMemberCache.TryGetValue(memberTuple, out attributes)) { return attributes; @@ -175,7 +172,7 @@ namespace System.ComponentModel.DataAnnotations // Only public static/instance members BindingFlags searchFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; // Try to find a matching member on type - MemberInfo matchingMember = type.GetMember(memberName, allowedMemberTypes, searchFlags).FirstOrDefault(); + MemberInfo? matchingMember = type.GetMember(memberName, allowedMemberTypes, searchFlags).FirstOrDefault(); if (matchingMember != null) { attributes = Attribute.GetCustomAttributes(matchingMember, true /* inherit */); -- 2.7.4