From 8d6f354a871b15d984a998c6b910831761626eed Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 23 Aug 2021 10:03:49 -0400 Subject: [PATCH] Fix nullable annotations for AssociatedMetadataTypeTypeDescriptionProvider --- .../ref/System.ComponentModel.Annotations.cs | 5 +--- ...atedMetadataTypeTypeDescriptionProvider.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 aa1912cdb0f9d7..c78333ddf64333 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 a408e38342083e..fbfe36a6c888b8 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 @@ public AssociatedMetadataTypeTypeDescriptionProvider( /// 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 5c49bd6062f710..ab8b86b13161df 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 @@ public AssociatedMetadataTypeTypeDescriptor( } [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 @@ public override AttributeCollection GetAttributes() 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 @@ public static void ValidateMetadataType( } [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 @@ public static Attribute[] GetAssociatedMetadata( string memberName) { (Type, string) memberTuple = (type, memberName); - Attribute[] attributes; + Attribute[]? attributes; if (s_typeMemberCache.TryGetValue(memberTuple, out attributes)) { return attributes; @@ -175,7 +172,7 @@ public static Attribute[] GetAssociatedMetadata( // 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 */);