Skip to content

[AOT] Make BindConverter safe for AOT or mark as unsafe #45578

Closed as not planned
@Nick-Stanton

Description

@Nick-Stanton

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

BindConverter.cs has a ton of DynamicallyAccessedMembers.All annotations. It seems the class barely made the cut for trim safety, but definitely will not work in its current state for AOT.

Why not just mark those methods as requiring dynamic code and therefore unsafe for AOT?

Unfortunately, our forms input classes implement BindConverter.TryConvertTo<TValue>(), and to mark those as unsafe puts us in a jam since their implementation of BuildRenderTree() must have the same annotations as the version in base class ComponentBase.cs. Clearly, we can't take dynamic code annotations up the call chain unless we want large parts of Blazor to be marked as unsafe for AOT.

Why does BindConverter need so many DynamicallyAccessedMembers annotations?

The use of TypeDescriptor.GetConverter() (see here and here) seem to be the culprit and to my knowledge there will be no effort to remove any dynamic code annotations from there.

Describe the solution you'd like

I see a few different options here:

  • Go ahead and mark everything BindConverter touches (and the subsequent warnings that bubble up the call chain) as unsafe for AOT. This would create an obstacle to Blazor being a useful framework for Native AOT solutions.
  • Get BindConverter to a point where it works without the DynamicallyAccessedMembers.All annotations, which would require the removal of TypeDescriptor.GetConverter().
  • Mark BindConverter as unsafe and create safe paths for classes that use BindConverter but also want to enable AOT by using an if-else block that checks for RuntimeFeature.IsDynamicCodeCompiled.

Additional context

CC @JamesNK @eerhardt for any advice, additional context, or cautionary tales surrounding this.

Part of effort tracked in #45473

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions