diff --git a/src/libraries/System.ComponentModel.TypeConverter/ref/System.ComponentModel.TypeConverter.cs b/src/libraries/System.ComponentModel.TypeConverter/ref/System.ComponentModel.TypeConverter.cs index 41cc1051338d42..4963ec94e306c2 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/ref/System.ComponentModel.TypeConverter.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/ref/System.ComponentModel.TypeConverter.cs @@ -289,7 +289,7 @@ protected CustomTypeDescriptor(System.ComponentModel.ICustomTypeDescriptor? pare public virtual string? GetClassName() { throw null; } public virtual string? GetComponentName() { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")] - public virtual System.ComponentModel.TypeConverter GetConverter() { throw null; } + public virtual System.ComponentModel.TypeConverter? GetConverter() { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The built-in EventDescriptor implementation uses Reflection which requires unreferenced code.")] public virtual System.ComponentModel.EventDescriptor? GetDefaultEvent() { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("PropertyDescriptor's PropertyType cannot be statically discovered.")] @@ -584,7 +584,7 @@ public partial interface ICustomTypeDescriptor string? GetClassName(); string? GetComponentName(); [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")] - System.ComponentModel.TypeConverter GetConverter(); + System.ComponentModel.TypeConverter? GetConverter(); [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The built-in EventDescriptor implementation uses Reflection which requires unreferenced code.")] System.ComponentModel.EventDescriptor? GetDefaultEvent(); [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("PropertyDescriptor's PropertyType cannot be statically discovered.")] diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/CustomTypeDescriptor.cs b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/CustomTypeDescriptor.cs index 7adad3b3bc0dc3..bc5c5ce6ffa67e 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/CustomTypeDescriptor.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/CustomTypeDescriptor.cs @@ -70,7 +70,7 @@ public virtual AttributeCollection GetAttributes() /// descriptor is representing. /// [RequiresUnreferencedCode(TypeConverter.RequiresUnreferencedCodeMessage)] - public virtual TypeConverter GetConverter() + public virtual TypeConverter? GetConverter() { if (_parent != null) { diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ICustomTypeDescriptor.cs b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ICustomTypeDescriptor.cs index 57b602ac9e9636..65c7db6623a25e 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ICustomTypeDescriptor.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ICustomTypeDescriptor.cs @@ -30,7 +30,7 @@ public interface ICustomTypeDescriptor /// Gets a type converter for this object. /// [RequiresUnreferencedCode(TypeConverter.RequiresUnreferencedCodeMessage)] - TypeConverter GetConverter(); + TypeConverter? GetConverter(); /// /// Gets the default event for this object. diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs index 9d965f44185f83..4bc9feca4d15db 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs @@ -599,7 +599,7 @@ public static AttributeCollection GetAttributes([DynamicallyAccessedMembers(Dyna return new AttributeCollection((Attribute[])null); } - AttributeCollection attributes = GetDescriptor(componentType, nameof(componentType))!.GetAttributes(); + AttributeCollection attributes = GetDescriptor(componentType, nameof(componentType)).GetAttributes(); return attributes; } @@ -725,7 +725,7 @@ public static AttributeCollection GetAttributes(object component, bool noCustomT public static string? GetClassName( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType) { - return GetDescriptor(componentType, nameof(componentType))!.GetClassName(); + return GetDescriptor(componentType, nameof(componentType)).GetClassName(); } /// @@ -757,7 +757,10 @@ public static AttributeCollection GetAttributes(object component, bool noCustomT [RequiresUnreferencedCode(TypeConverter.RequiresUnreferencedCodeMessage + " The Type of component cannot be statically discovered.")] public static TypeConverter GetConverter(object component, bool noCustomTypeDesc) { - TypeConverter converter = GetDescriptor(component, noCustomTypeDesc)!.GetConverter(); + TypeConverter? converter = GetDescriptor(component, noCustomTypeDesc)!.GetConverter(); + // GetDescriptor will only return DefaultTypeDescriptor, or MergedTypeDescriptor with DefaultTypeDescriptor as the secondary, + // which will always return a non-null TypeConverter. + Debug.Assert(converter != null, "Unexpected null TypeConverter."); return converter; } @@ -767,7 +770,7 @@ public static TypeConverter GetConverter(object component, bool noCustomTypeDesc [RequiresUnreferencedCode(TypeConverter.RequiresUnreferencedCodeMessage)] public static TypeConverter GetConverter([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type) { - return GetDescriptor(type, nameof(type))!.GetConverter(); + return GetDescriptor(type, nameof(type)).GetConverter(); } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", @@ -795,7 +798,7 @@ internal static TypeConverter GetConverterTrimUnsafe([DynamicallyAccessedMembers return null; } - return GetDescriptor(componentType, nameof(componentType))!.GetDefaultEvent(); + return GetDescriptor(componentType, nameof(componentType)).GetDefaultEvent(); } /// @@ -833,7 +836,7 @@ internal static TypeConverter GetConverterTrimUnsafe([DynamicallyAccessedMembers return null; } - return GetDescriptor(componentType, nameof(componentType))!.GetDefaultProperty(); + return GetDescriptor(componentType, nameof(componentType)).GetDefaultProperty(); } /// @@ -862,13 +865,13 @@ internal static TypeConverter GetConverterTrimUnsafe([DynamicallyAccessedMembers /// Returns a custom type descriptor for the given type. /// Performs arg checking so callers don't have to. /// - internal static ICustomTypeDescriptor? GetDescriptor( + private static DefaultTypeDescriptor GetDescriptor( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type, string typeName) { - ArgumentNullException.ThrowIfNull(type); + ArgumentNullException.ThrowIfNull(type, typeName); - return NodeFor(type).GetTypeDescriptor(type); + return NodeFor(type).GetDefaultTypeDescriptor(type); } /// @@ -942,7 +945,7 @@ internal static ICustomTypeDescriptor GetExtendedDescriptor(object component) { ArgumentNullException.ThrowIfNull(editorBaseType); - return GetDescriptor(type, nameof(type))!.GetEditor(editorBaseType); + return GetDescriptor(type, nameof(type)).GetEditor(editorBaseType); } /// @@ -975,7 +978,7 @@ public static EventDescriptorCollection GetEvents( return new EventDescriptorCollection(null, true); } - EventDescriptorCollection events = GetDescriptor(componentType, nameof(componentType))!.GetEvents(attributes); + EventDescriptorCollection events = GetDescriptor(componentType, nameof(componentType)).GetEvents(attributes); if (attributes != null && attributes.Length > 0) { @@ -1185,7 +1188,7 @@ public static PropertyDescriptorCollection GetProperties( return new PropertyDescriptorCollection(null, true); } - return GetDescriptor(componentType, nameof(componentType))!.GetProperties(); + return GetDescriptor(componentType, nameof(componentType)).GetProperties(); } /// @@ -1203,7 +1206,7 @@ public static PropertyDescriptorCollection GetProperties( return new PropertyDescriptorCollection(null, true); } - PropertyDescriptorCollection properties = GetDescriptor(componentType, nameof(componentType))!.GetProperties(attributes); + PropertyDescriptorCollection properties = GetDescriptor(componentType, nameof(componentType)).GetProperties(attributes); if (attributes != null && attributes.Length > 0) { @@ -2890,7 +2893,7 @@ string ICustomTypeDescriptor.GetClassName() [RequiresUnreferencedCode(TypeConverter.RequiresUnreferencedCodeMessage)] TypeConverter ICustomTypeDescriptor.GetConverter() { - TypeConverter converter = _primary.GetConverter() ?? _secondary.GetConverter(); + TypeConverter? converter = _primary.GetConverter() ?? _secondary.GetConverter(); Debug.Assert(converter != null, "Someone should have handled this"); return converter; @@ -3116,6 +3119,11 @@ public override ICustomTypeDescriptor GetTypeDescriptor([DynamicallyAccessedMemb return new DefaultTypeDescriptor(this, objectType, instance); } + internal DefaultTypeDescriptor GetDefaultTypeDescriptor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType) + { + return new DefaultTypeDescriptor(this, objectType, instance: null); + } + public override bool IsSupportedType(Type type) { ArgumentNullException.ThrowIfNull(type); @@ -3226,7 +3234,7 @@ TypeConverter ICustomTypeDescriptor.GetConverter() ICustomTypeDescriptor desc = p.GetExtendedTypeDescriptor(_instance); if (desc == null) throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetExtendedTypeDescriptor")); - TypeConverter converter = desc.GetConverter(); + TypeConverter? converter = desc.GetConverter(); if (converter == null) throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetConverter")); return converter; } @@ -3415,350 +3423,350 @@ object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor? pd) return owner; } } + } + + /// + /// The default type descriptor. + /// + private readonly struct DefaultTypeDescriptor : ICustomTypeDescriptor + { + private readonly TypeDescriptionNode _node; + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + private readonly Type _objectType; + private readonly object? _instance; /// - /// The default type descriptor. + /// Creates a new WalkingTypeDescriptor. /// - private readonly struct DefaultTypeDescriptor : ICustomTypeDescriptor + internal DefaultTypeDescriptor( + TypeDescriptionNode node, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType, + object? instance) { - private readonly TypeDescriptionNode _node; - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - private readonly Type _objectType; - private readonly object? _instance; + _node = node; + _objectType = objectType; + _instance = instance; + } - /// - /// Creates a new WalkingTypeDescriptor. - /// - internal DefaultTypeDescriptor( - TypeDescriptionNode node, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType, - object? instance) + /// + /// ICustomTypeDescriptor implementation. + /// + public AttributeCollection GetAttributes() + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + AttributeCollection attrs; + if (p is ReflectTypeDescriptionProvider rp) { - _node = node; - _objectType = objectType; - _instance = instance; + attrs = rp.GetAttributes(_objectType); } - - /// - /// ICustomTypeDescriptor implementation. - /// - AttributeCollection ICustomTypeDescriptor.GetAttributes() + else { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - AttributeCollection attrs; - if (p is ReflectTypeDescriptionProvider rp) - { - attrs = rp.GetAttributes(_objectType); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - attrs = desc.GetAttributes(); - if (attrs == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetAttributes")); - } - - return attrs; + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + attrs = desc.GetAttributes(); + if (attrs == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetAttributes")); } - /// - /// ICustomTypeDescriptor implementation. - /// - string? ICustomTypeDescriptor.GetClassName() - { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - string? name; - if (p is ReflectTypeDescriptionProvider rp) - { - name = rp.GetClassName(_objectType); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - name = desc.GetClassName() ?? _objectType.FullName; - } + return attrs; + } - return name; + /// + /// ICustomTypeDescriptor implementation. + /// + public string? GetClassName() + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + string? name; + if (p is ReflectTypeDescriptionProvider rp) + { + name = rp.GetClassName(_objectType); } - - /// - /// ICustomTypeDescriptor implementation. - /// - string? ICustomTypeDescriptor.GetComponentName() + else { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - string? name; - if (p is ReflectTypeDescriptionProvider) - { - name = ReflectTypeDescriptionProvider.GetComponentName(_instance); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - name = desc.GetComponentName(); - } - - return name; + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + name = desc.GetClassName() ?? _objectType.FullName; } - /// - /// ICustomTypeDescriptor implementation. - /// - [RequiresUnreferencedCode(TypeConverter.RequiresUnreferencedCodeMessage)] - TypeConverter ICustomTypeDescriptor.GetConverter() - { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - TypeConverter converter; - if (p is ReflectTypeDescriptionProvider rp) - { - converter = rp.GetConverter(_objectType, _instance); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - converter = desc.GetConverter(); - if (converter == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetConverter")); - } + return name; + } - return converter; + /// + /// ICustomTypeDescriptor implementation. + /// + string? ICustomTypeDescriptor.GetComponentName() + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + string? name; + if (p is ReflectTypeDescriptionProvider) + { + name = ReflectTypeDescriptionProvider.GetComponentName(_instance); } - - /// - /// ICustomTypeDescriptor implementation. - /// - [RequiresUnreferencedCode(EventDescriptor.RequiresUnreferencedCodeMessage)] - EventDescriptor? ICustomTypeDescriptor.GetDefaultEvent() + else { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - EventDescriptor? defaultEvent; - if (p is ReflectTypeDescriptionProvider rp) - { - defaultEvent = rp.GetDefaultEvent(_objectType, _instance); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - defaultEvent = desc.GetDefaultEvent(); - } - - return defaultEvent; + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + name = desc.GetComponentName(); } - /// - /// ICustomTypeDescriptor implementation. - /// - [RequiresUnreferencedCode(PropertyDescriptor.PropertyDescriptorPropertyTypeMessage)] - PropertyDescriptor? ICustomTypeDescriptor.GetDefaultProperty() - { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - PropertyDescriptor? defaultProperty; - if (p is ReflectTypeDescriptionProvider rp) - { - defaultProperty = rp.GetDefaultProperty(_objectType, _instance); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - defaultProperty = desc.GetDefaultProperty(); - } + return name; + } - return defaultProperty; + /// + /// ICustomTypeDescriptor implementation. + /// + [RequiresUnreferencedCode(TypeConverter.RequiresUnreferencedCodeMessage)] + public TypeConverter GetConverter() + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + TypeConverter? converter; + if (p is ReflectTypeDescriptionProvider rp) + { + converter = rp.GetConverter(_objectType, _instance); } - - /// - /// ICustomTypeDescriptor implementation. - /// - [RequiresUnreferencedCode(EditorRequiresUnreferencedCode)] - object? ICustomTypeDescriptor.GetEditor(Type editorBaseType) + else { - ArgumentNullException.ThrowIfNull(editorBaseType); + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + converter = desc.GetConverter(); + if (converter == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetConverter")); + } - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - object? editor; - if (p is ReflectTypeDescriptionProvider rp) - { - editor = rp.GetEditor(_objectType, _instance, editorBaseType); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - editor = desc.GetEditor(editorBaseType); - } + return converter; + } - return editor; + /// + /// ICustomTypeDescriptor implementation. + /// + [RequiresUnreferencedCode(EventDescriptor.RequiresUnreferencedCodeMessage)] + public EventDescriptor? GetDefaultEvent() + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + EventDescriptor? defaultEvent; + if (p is ReflectTypeDescriptionProvider rp) + { + defaultEvent = rp.GetDefaultEvent(_objectType, _instance); } - - /// - /// ICustomTypeDescriptor implementation. - /// - EventDescriptorCollection ICustomTypeDescriptor.GetEvents() + else { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - EventDescriptorCollection events; - if (p is ReflectTypeDescriptionProvider rp) - { - events = rp.GetEvents(_objectType); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - events = desc.GetEvents(); - if (events == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetEvents")); - } - - return events; + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + defaultEvent = desc.GetDefaultEvent(); } - /// - /// ICustomTypeDescriptor implementation. - /// - [RequiresUnreferencedCode(AttributeCollection.FilterRequiresUnreferencedCodeMessage)] - EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[]? attributes) - { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - EventDescriptorCollection events; - if (p is ReflectTypeDescriptionProvider rp) - { - events = rp.GetEvents(_objectType); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - events = desc.GetEvents(attributes); - if (events == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetEvents")); - } + return defaultEvent; + } - return events; + /// + /// ICustomTypeDescriptor implementation. + /// + [RequiresUnreferencedCode(PropertyDescriptor.PropertyDescriptorPropertyTypeMessage)] + public PropertyDescriptor? GetDefaultProperty() + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + PropertyDescriptor? defaultProperty; + if (p is ReflectTypeDescriptionProvider rp) + { + defaultProperty = rp.GetDefaultProperty(_objectType, _instance); + } + else + { + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + defaultProperty = desc.GetDefaultProperty(); } - /// - /// ICustomTypeDescriptor implementation. - /// - [RequiresUnreferencedCode(PropertyDescriptor.PropertyDescriptorPropertyTypeMessage)] - PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() + return defaultProperty; + } + + /// + /// ICustomTypeDescriptor implementation. + /// + [RequiresUnreferencedCode(EditorRequiresUnreferencedCode)] + public object? GetEditor(Type editorBaseType) + { + ArgumentNullException.ThrowIfNull(editorBaseType); + + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + object? editor; + if (p is ReflectTypeDescriptionProvider rp) { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - PropertyDescriptorCollection properties; - if (p is ReflectTypeDescriptionProvider rp) - { - properties = rp.GetProperties(_objectType); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - properties = desc.GetProperties(); - if (properties == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetProperties")); - } + editor = rp.GetEditor(_objectType, _instance, editorBaseType); + } + else + { + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + editor = desc.GetEditor(editorBaseType); + } - return properties; + return editor; + } + + /// + /// ICustomTypeDescriptor implementation. + /// + public EventDescriptorCollection GetEvents() + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + EventDescriptorCollection events; + if (p is ReflectTypeDescriptionProvider rp) + { + events = rp.GetEvents(_objectType); + } + else + { + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + events = desc.GetEvents(); + if (events == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetEvents")); } - /// - /// ICustomTypeDescriptor implementation. - /// - [RequiresUnreferencedCode(PropertyDescriptor.PropertyDescriptorPropertyTypeMessage + " " + AttributeCollection.FilterRequiresUnreferencedCodeMessage)] - PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[]? attributes) + return events; + } + + /// + /// ICustomTypeDescriptor implementation. + /// + [RequiresUnreferencedCode(AttributeCollection.FilterRequiresUnreferencedCodeMessage)] + public EventDescriptorCollection GetEvents(Attribute[]? attributes) + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + EventDescriptorCollection events; + if (p is ReflectTypeDescriptionProvider rp) { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - PropertyDescriptorCollection properties; - if (p is ReflectTypeDescriptionProvider rp) - { - properties = rp.GetProperties(_objectType); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - properties = desc.GetProperties(attributes); - if (properties == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetProperties")); - } + events = rp.GetEvents(_objectType); + } + else + { + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + events = desc.GetEvents(attributes); + if (events == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetEvents")); + } - return properties; + return events; + } + + /// + /// ICustomTypeDescriptor implementation. + /// + [RequiresUnreferencedCode(PropertyDescriptor.PropertyDescriptorPropertyTypeMessage)] + public PropertyDescriptorCollection GetProperties() + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + PropertyDescriptorCollection properties; + if (p is ReflectTypeDescriptionProvider rp) + { + properties = rp.GetProperties(_objectType); + } + else + { + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + properties = desc.GetProperties(); + if (properties == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetProperties")); } - /// - /// ICustomTypeDescriptor implementation. - /// - object? ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor? pd) + return properties; + } + + /// + /// ICustomTypeDescriptor implementation. + /// + [RequiresUnreferencedCode(PropertyDescriptor.PropertyDescriptorPropertyTypeMessage + " " + AttributeCollection.FilterRequiresUnreferencedCodeMessage)] + public PropertyDescriptorCollection GetProperties(Attribute[]? attributes) + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + PropertyDescriptorCollection properties; + if (p is ReflectTypeDescriptionProvider rp) { - // Check to see if the provider we get is a ReflectTypeDescriptionProvider. - // If so, we can call on it directly rather than creating another - // custom type descriptor - TypeDescriptionProvider p = _node.Provider; - object? owner; - if (p is ReflectTypeDescriptionProvider) - { - owner = ReflectTypeDescriptionProvider.GetPropertyOwner(_objectType, _instance!, pd); - } - else - { - ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); - if (desc == null) - throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); - owner = desc.GetPropertyOwner(pd) ?? _instance; - } + properties = rp.GetProperties(_objectType); + } + else + { + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + properties = desc.GetProperties(attributes); + if (properties == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetProperties")); + } - return owner; + return properties; + } + + /// + /// ICustomTypeDescriptor implementation. + /// + object? ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor? pd) + { + // Check to see if the provider we get is a ReflectTypeDescriptionProvider. + // If so, we can call on it directly rather than creating another + // custom type descriptor + TypeDescriptionProvider p = _node.Provider; + object? owner; + if (p is ReflectTypeDescriptionProvider) + { + owner = ReflectTypeDescriptionProvider.GetPropertyOwner(_objectType, _instance!, pd); } + else + { + ICustomTypeDescriptor? desc = p.GetTypeDescriptor(_objectType, _instance); + if (desc == null) + throw new InvalidOperationException(SR.Format(SR.TypeDescriptorProviderError, _node.Provider.GetType().FullName, "GetTypeDescriptor")); + owner = desc.GetPropertyOwner(pd) ?? _instance; + } + + return owner; } } diff --git a/src/libraries/System.Data.Common/src/System/Data/Common/DataRecordInternal.cs b/src/libraries/System.Data.Common/src/System/Data/Common/DataRecordInternal.cs index e148813f371bd6..825036afdf6b43 100644 --- a/src/libraries/System.Data.Common/src/System/Data/Common/DataRecordInternal.cs +++ b/src/libraries/System.Data.Common/src/System/Data/Common/DataRecordInternal.cs @@ -318,9 +318,9 @@ AttributeCollection ICustomTypeDescriptor.GetAttributes() } [RequiresUnreferencedCode("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")] - TypeConverter ICustomTypeDescriptor.GetConverter() + TypeConverter? ICustomTypeDescriptor.GetConverter() { - return null!; + return null; } [RequiresUnreferencedCode("The built-in EventDescriptor implementation uses Reflection which requires unreferenced code.")] diff --git a/src/libraries/System.Data.Common/src/System/Data/Common/DbDataRecord.cs b/src/libraries/System.Data.Common/src/System/Data/Common/DbDataRecord.cs index 14f3d84387724b..f6211bfe3fe8fa 100644 --- a/src/libraries/System.Data.Common/src/System/Data/Common/DbDataRecord.cs +++ b/src/libraries/System.Data.Common/src/System/Data/Common/DbDataRecord.cs @@ -77,7 +77,7 @@ protected virtual DbDataReader GetDbDataReader(int i) string? ICustomTypeDescriptor.GetComponentName() => null; [RequiresUnreferencedCode("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")] - TypeConverter ICustomTypeDescriptor.GetConverter() => null!; + TypeConverter? ICustomTypeDescriptor.GetConverter() => null; [RequiresUnreferencedCode("The built-in EventDescriptor implementation uses Reflection which requires unreferenced code.")] EventDescriptor? ICustomTypeDescriptor.GetDefaultEvent() => null; diff --git a/src/libraries/System.Data.Common/src/System/Data/DataRowView.cs b/src/libraries/System.Data.Common/src/System/Data/DataRowView.cs index 8784f35284a0af..453049522da432 100644 --- a/src/libraries/System.Data.Common/src/System/Data/DataRowView.cs +++ b/src/libraries/System.Data.Common/src/System/Data/DataRowView.cs @@ -234,7 +234,7 @@ public void EndEdit() string? ICustomTypeDescriptor.GetComponentName() => null; [RequiresUnreferencedCode("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")] - TypeConverter ICustomTypeDescriptor.GetConverter() => null!; + TypeConverter? ICustomTypeDescriptor.GetConverter() => null; [RequiresUnreferencedCode("The built-in EventDescriptor implementation uses Reflection which requires unreferenced code.")] EventDescriptor? ICustomTypeDescriptor.GetDefaultEvent() => null; diff --git a/src/libraries/System.Data.Common/src/System/Data/DataViewManagerListItemTypeDescriptor.cs b/src/libraries/System.Data.Common/src/System/Data/DataViewManagerListItemTypeDescriptor.cs index d991f29e5f1e1c..afa7277482b18f 100644 --- a/src/libraries/System.Data.Common/src/System/Data/DataViewManagerListItemTypeDescriptor.cs +++ b/src/libraries/System.Data.Common/src/System/Data/DataViewManagerListItemTypeDescriptor.cs @@ -49,7 +49,7 @@ internal DataView GetDataView(DataTable table) /// Retrieves the type converter for this object. /// [RequiresUnreferencedCode("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")] - TypeConverter ICustomTypeDescriptor.GetConverter() => null!; + TypeConverter? ICustomTypeDescriptor.GetConverter() => null; /// /// Retrieves the default event.