Fix nullable annotations for AssociatedMetadataTypeTypeDescriptionProvider (#57979)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Tue, 24 Aug 2021 22:43:22 +0000 (16:43 -0600)
committerGitHub <noreply@github.com>
Tue, 24 Aug 2021 22:43:22 +0000 (16:43 -0600)
Co-authored-by: Stephen Toub <stoub@microsoft.com>
src/libraries/System.ComponentModel.Annotations/ref/System.ComponentModel.Annotations.cs
src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptionProvider.cs
src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/AssociatedMetadataTypeTypeDescriptor.cs

index aa1912c..c78333d 100644 (file)
@@ -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
index a408e38..fbfe36a 100644 (file)
@@ -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;
 
         /// <summary>
         /// Initializes a new instance of the System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider
@@ -53,9 +50,9 @@ namespace System.ComponentModel.DataAnnotations
         /// <param name="objectType">The type of object to retrieve the type descriptor for.</param>
         /// <param name="instance">An instance of the type.</param>
         /// <returns>The descriptor that provides metadata for the type.</returns>
-        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);
         }
     }
index 5c49bd6..ab8b86b 100644 (file)
@@ -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<Type, Type> s_metadataTypeCache = new ConcurrentDictionary<Type, Type>();
+            private static readonly ConcurrentDictionary<Type, Type?> s_metadataTypeCache = new ConcurrentDictionary<Type, Type?>();
 
             // 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 */);