Repository navigation
Conversation
cce199b to
0e67e50
Compare
0e67e50 to
36dfc1c
Compare
e1ca242 to
5cce742
Compare
Use non-generic enum parsing and reflectionless array conversion so runtime-selected binding no longer depends on emitted generic closures. Behavior: changed: enum and array binding works without dynamic code State: builds; tests in #2 Review hint: compare the dynamic-code-safe enum and array branches with their existing generic counterparts Co-authored-by: Copilot App <[email protected]>
Compare the reflectionless enum and array paths with the existing JIT conversion behavior. Behavior: preserved State: complete Co-authored-by: Copilot App <[email protected]>
Evaluate known binding expression shapes through the MemberInfo already carried by each expression node, preserving the existing fallback for unsupported shapes. Behavior: preserved State: builds; reflection walk tests in #4 Review hint: this commit deliberately has no bindable descriptor abstraction; every recognized hop uses expression metadata Co-authored-by: Copilot App <[email protected]>
Cover nested members, fields carried by closures, arrays, indexers, edit-context anchoring, and compatibility with FieldIdentifier.Create. Behavior: preserved State: complete Co-authored-by: Copilot App <[email protected]>
Introduce bindable member, indexer, and type descriptors at their first generated use. Registered metadata contexts now resolve descriptor graphs before the existing reflection fallback. The existing application metadata generator emits only JsonTypeInfoResolver, JSInvokableMethods, and BindableTypes at this layer. Behavior: changed: described form models use generated accessors while undescribed models retain reflection compatibility State: builds; generated graph and resolver tests in #6 Review hint: generator changes are limited to bindable discovery, accessors, context registration, and descriptor-first expression evaluation Co-authored-by: Copilot App <[email protected]>
Cover transitive and cyclic model discovery, private member accessors, indexers, descriptor-first expression walks, and per-provider resolver composition. Behavior: preserved State: complete Review hint: generated tests exercise descriptors as runtime delegates, not only emitted source text Co-authored-by: Copilot App <[email protected]>
5cce742 to
1c5e0b8
Compare
There was a problem hiding this comment.
Pull request overview
This PR extends the Blazor “metadata-first” pipeline to cover interactive form binding under Native AOT by generating bindable model descriptors and consuming them at runtime to avoid Expression.Compile/runtime generic closure for supported shapes, while preserving reflection fallback for compatibility.
Changes:
- Adds an experimental bindable metadata contract (
BindableTypeDescriptor+BindableModelAttribute) and flows it throughRazorComponentsMetadataContextand DI viaIBindableTypeResolver. - Introduces a binding-expression walker (
BindingExpressionEvaluator) used by form components to resolveFieldIdentifierwithout compiling delegates. - Updates
BindConverterenum/array parsing+formatting to choose dynamic-code-safe implementations whenRuntimeFeature.IsDynamicCodeSupportedis false, and extends the Endpoints generator to emit bindable graphs plus tests.
Reviewed changes
Copilot reviewed 39 out of 39 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Components/WebAssembly/WebAssembly/test/Services/WebAssemblyHostSerializationContextTest.cs | Updates test contexts to implement the new BindableTypes metadata surface. |
| src/Components/Web/test/Microsoft.AspNetCore.Components.Web.Tests.csproj | Suppresses experimental warning ASPNETCORE9004 in Web tests that touch new experimental APIs. |
| src/Components/Web/test/Metadata/ComponentMetadataServiceCollectionExtensionsTest.cs | Verifies DI composition/isolation for both JSON resolvers and bindable type resolver. |
| src/Components/Web/test/Forms/BindingExpressionEvaluatorTest.cs | Adds unit tests for expression walking with descriptor-assisted and reflection fallback evaluation. |
| src/Components/Web/src/PublicAPI.Unshipped.txt | Records new/updated public API surface in Components.Web (BindableModelAttribute, RazorComponentsMetadataContext.BindableTypes). |
| src/Components/Web/src/Microsoft.AspNetCore.Components.Web.csproj | Suppresses ASPNETCORE9004 within Components.Web (declares/consumes experimental APIs). |
| src/Components/Web/src/Metadata/RazorComponentsMetadataContext.cs | Adds BindableTypes to the metadata context contract. |
| src/Components/Web/src/Metadata/ComponentMetadataServiceCollectionExtensions.cs | Registers ComponentBindableTypeResolver/IBindableTypeResolver alongside existing metadata services. |
| src/Components/Web/src/Metadata/ComponentBindableTypeResolver.cs | New resolver that composes BindableTypeDescriptors across registered metadata contexts. |
| src/Components/Web/src/Metadata/BindableModelAttribute.cs | New experimental attribute to declare form-model roots for generator graph discovery. |
| src/Components/Web/src/Forms/ValidationMessage.cs | Switches FieldIdentifier creation to the evaluator using the renderer-scoped bindable resolver. |
| src/Components/Web/src/Forms/InputSelect.cs | Adds AOT suppression rationale for array-related binding in BuildRenderTree. |
| src/Components/Web/src/Forms/InputBase.cs | Switches FieldIdentifier creation to the evaluator using the renderer-scoped bindable resolver. |
| src/Components/Web/src/Forms/BindingExpressionEvaluator.cs | New internal evaluator that walks member/indexer/array-index chains without compiling expressions. |
| src/Components/Shared/src/ExpressionFormatting/ExpressionFormatter.cs | Avoids Expression.Compile for captured index formatting when dynamic code isn’t supported. |
| src/Components/Server/test/ProtectedBrowserStorageSerializerOptionsTest.cs | Updates stub metadata contexts to implement BindableTypes. |
| src/Components/Server/test/Circuits/RemoteJSRuntimeMetadataTest.cs | Updates stub metadata contexts to implement BindableTypes. |
| src/Components/Endpoints/test/Microsoft.AspNetCore.Components.Endpoints.Generators.Tests/RazorComponentsMetadataGeneratorBindableTests.cs | Adds generator tests validating bindable graph emission (members, indexers, cycles, stability). |
| src/Components/Endpoints/test/Microsoft.AspNetCore.Components.Endpoints.Generators.Tests/Microsoft.AspNetCore.Components.Endpoints.Generators.Tests.csproj | Moves xUnit global-using removal into a target to avoid ordering issues. |
| src/Components/Endpoints/test/DependencyInjection/ComponentJsonMetadataIsolationTest.cs | Updates test contexts to implement BindableTypes. |
| src/Components/Endpoints/test/Binding/FormDataMapperTests.cs | Adds reflection-based assertions around dynamic-code/unreferenced-code annotations on constructors. |
| src/Components/Endpoints/src/FormMapping/FormDataMapperOptions.cs | Removes dynamic-code/unreferenced-code attributes from constructors to reduce AOT friction. |
| src/Components/Endpoints/gen/WellKnownTypes.cs | Adds well-known type lookup for BindableModelAttribute. |
| src/Components/Endpoints/gen/RazorComponentsMetadataGenerator.cs | Integrates bindable type collection into generator model and expands diagnostic payload. |
| src/Components/Endpoints/gen/RazorComponentsMetadataGenerator.Bindables.cs | New bindable graph collector (BFS + leaf filtering + accessibility checks). |
| src/Components/Endpoints/gen/Models/MetadataContextModel.cs | Extends generator model with bindable type/member/indexer records. |
| src/Components/Endpoints/gen/Emitters/RazorComponentsMetadataGenerator.Emitter.cs | Emits BindableTypes descriptors + optional UnsafeAccessor bridges. |
| src/Components/Endpoints/gen/DiagnosticDescriptors.cs | Adds BLAZORAOT002 for bindable model description failures. |
| src/Components/Components/test/BindConverterTest.cs | Adds coverage for enum and array conversion/formatting parity, including “no dynamic code” remote execution. |
| src/Components/Components/src/RenderTree/Renderer.cs | Captures IBindableTypeResolver from DI for renderer-scoped access. |
| src/Components/Components/src/RenderHandle.cs | Exposes the renderer-scoped bindable resolver via RenderHandle. |
| src/Components/Components/src/PublicAPI.Unshipped.txt | Records new experimental binding descriptor APIs in Components. |
| src/Components/Components/src/Microsoft.AspNetCore.Components.csproj | Suppresses ASPNETCORE9004 within Components (declares/consumes experimental APIs). |
| src/Components/Components/src/Infrastructure/IBindableTypeResolver.cs | New internal contract for runtime lookup of bindable type descriptors. |
| src/Components/Components/src/Infrastructure/BindableTypeDescriptor.cs | New experimental descriptor for members/indexers a binding expression may traverse. |
| src/Components/Components/src/Infrastructure/BindableMemberDescriptor.cs | New experimental descriptor for member name/type + getter delegate. |
| src/Components/Components/src/Infrastructure/BindableIndexerDescriptor.cs | New experimental descriptor for single-arg indexers + getter delegate. |
| src/Components/Components/src/ComponentBase.cs | Exposes internal Handle to allow friend assemblies to access renderer-scoped services without exposing the handle publicly. |
| src/Components/Components/src/BindConverter.cs | Adds dynamic-code-safe enum/nullable-enum and array parse/format paths and supporting helpers for AOT scenarios. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
| if (obj is null) | ||
| { | ||
| value = null; | ||
| return true; | ||
| } | ||
|
|
||
| value = converter.ConvertFrom(context: null, culture ?? CultureInfo.CurrentCulture, obj); | ||
| return true; |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Overview
Stack layer 3/6 for #68332, stacked on #68296. Across six commits and 39 files, this change removes runtime generic closure from enum/array binding, evaluates supported form expressions without compiling them, and extends the existing application metadata generator with bindable model graphs and accessors.
The cross-cutting constraint is generated first, reflection last: described models use statically analyzable delegates, while undescribed models and unsupported expression shapes keep the existing compatibility fallback. This layer does not add component metadata, framework providers, strict-mode switches, or general FormMapping Native AOT support; the generator surface remains
JsonTypeInfoResolver,JSInvokableMethods, andBindableTypesonly.Design
Applications opt a normal C# model into generation explicitly. The explicit model root is required because source generators cannot observe Razor types produced by another generator in the same compilation; ordinary model types are visible and can seed the complete reachable graph.
The generated contract describes only the model-side hops a binding expression may traverse. The component-side prefix is deliberately not described: the evaluator anchors the walk at the
EditContext.Model, reducing metadata and avoiding a dependency on generated component types.BindableMemberDescriptorcarriesName,MemberType, andFunc<object, object?> GetValue;BindableIndexerDescriptorcarriesIndexType,ValueType, andFunc<object, object?, object?> GetValue. These sibling contracts use delegates rather thanMemberInfo, so generated access is statically analyzable and private members can be bridged with generatedUnsafeAccessormethods.The existing metadata context gains the bindable list at the same point it is first emitted and consumed:
The rejected alternatives were runtime
MakeGenericMethod/Expression.Compile(not available under Native AOT), describing component roots (unnecessary and unavailable to this generator layer), and removing reflection entirely (would break the default compatibility contract).Implementation
BindConverterretains its existing specialized generic delegates when dynamic code is available, but selects non-generic enum APIs and reflectionless array construction otherwise. Enum and nullable-enum parsing are one equivalence class; array parsing and formatting are the second.The AOT array path preserves the exact runtime array type—including nested and non-vector array shapes—then converts each element through the same non-generic element dispatcher. Formatting mirrors the same branch and JSON-escapes each formatted element. Nullable enums use the same non-generic
Enum.TryParse(Type, ...)pattern after recoveringNullable.GetUnderlyingType(typeof(T));InputSelect<TValue>contains only the narrow closed-path suppression beside its array binder.InputBase<TValue>andValidationMessage<TValue>now enter through one evaluator. It recognizes member access, single-argument indexers, and array indexing; unsupported shapes still callFieldIdentifier.Create, preserving previous behavior. Captured index values also avoidExpression.Compilewhen dynamic code is unavailable by reading thePropertyInfoorFieldInfoalready embedded in the expression.Generation follows the full bindable graph breadth-first, deduplicates cycles and inherited hidden members, follows array elements and generic type arguments, and stops at primitive/framework leaves. Directly accessible members become direct delegates; inaccessible properties and fields use generated
UnsafeAccessorbridges. Single-argument indexers are emitted as typed delegates, while unsupported/inaccessible members are omitted with a targeted diagnostic for an inaccessible root.Registration composes multiple metadata contexts once into a type-keyed resolver. The renderer carries that resolver through
RenderHandle, so form components use generated descriptors without introducing a process-static registry; later providers do not mutate an already-built service provider.Outcome
BindConverterTest: 41 passedFormDataMapperTests: 282 passedFieldIdentifier.CreateBindingExpressionEvaluatorTest: 9 passedUnsafeAccessor, indexers, inheritance/hiding, transitive arrays/collections, cycles, stable ordering, unsupported members, and per-provider resolver isolationTesttarget on MSTest/MTP: 15 passed, including all 5 bindable generator scenariosWasmLinkerTestbuild: 0 warnings, 0 errorsThe result is an AOT-safe path for the binding scenarios this layer owns, while default JIT behavior and reflection fallback remain intact.