Skip to content

GetType() behave strange #93720

@kant2002

Description

@kant2002

I attempt to annotate WinForms for linker.

I have code approximately like this

// [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] // fix
class DataGridViewCell
{
    public virtual object Clone()
    {
        DataGridViewCell dataGridViewCell = (DataGridViewCell)System.Activator.CreateInstance(GetType()); // no warning
        return dataGridViewCell;
    }
}

class DataGridViewCellConverter
{
  public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
  {
      ArgumentNullException.ThrowIfNull(destinationType);
  
      if (destinationType == typeof(InstanceDescriptor) && value is DataGridViewCell cell)
      {
          ConstructorInfo? ctor = cell.GetType().GetConstructor(Array.Empty<Type>());  // warning
          if (ctor is not null)
          {
              return new InstanceDescriptor(ctor, Array.Empty<object>(), false);
          }
      }
  
      return base.ConvertTo(context, culture, value, destinationType);
  }
}

If I add [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] to DataGridViewCell type, then issue in Clone goes away, but in Converter stays, and complain that it needs PublicParameterlessConstructor.

In my understanding these two versions of code absolutely identical and either I should have a fix for both places, or have warnings in both places.

https://github.com/dotnet/winforms/blob/5427959215dfc57d4ecf082ba5f65dc597e2c2b9/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCell.cs#L1106-L1111
and
https://github.com/dotnet/winforms/blob/5427959215dfc57d4ecf082ba5f65dc597e2c2b9/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCellConverter.cs#L25-L39

Metadata

Metadata

Assignees

Labels

area-Tools-ILLink.NET linker development as well as trimming analyzers

Type

No type

Projects

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions