Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 16, 2026

Summary of the changes

Implements automatic field inference for @ref attributes, eliminating manual field declarations in @code blocks. Fields are now auto-generated with correct types: ElementReference for HTML elements, component type for components.

Before:

<MyComponent @ref="myComponent" />

@code {
    private MyComponent myComponent;  // Manual declaration required
    void DoSomething() => myComponent.ShowMessage();
}

After:

<MyComponent @ref="myComponent" />

@code {
    void DoSomething() => myComponent.ShowMessage();  // Field auto-generated
}

Changes

  • Modified ComponentReferenceCaptureLoweringPass: Generates FieldDeclarationIntermediateNode for each @ref directive during compilation
  • Type inference: Automatically determines field type from parent node (component type vs ElementReference)
  • Duplicate prevention: Tracks generated fields and skips if field/property already exists
  • Insertion logic: Places generated fields after design-time setup code, before method declarations

Known Issues

Critical: Field nodes are not appearing in generated IR despite correct insertion logic. The AddFieldDeclaration method follows the same pattern as ViewComponentTagHelperPass but fields don't materialize. Possible causes:

  • Incorrect insertion index calculation
  • Pass ordering issue (fields added then removed)
  • Stale class node reference

Requires debugger access to inspect IR state across compilation stages.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • bcnvsblobprodcus378.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet build src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj --no-restore e.Ra�� dio.LegacyEditor.Razor.Test/Parsing/RazorSyntaxTreePartialParserTest.cs dio.LegacyEditor.Razor.Test/Parsing/VisualStudioRazorParserIntegrationTest.cs dio.LegacyEditor.Razor.Test/Parsing/VisualStudioRazorParserTest.cs dio.LegacyEditordirname dio.LegacyEditor/home/REDACTED/work/razor/razor/eng/common/tools.sh dio.LegacyEditor.Razor.Test/Completion/RazorDirectiveAttributeCompletionSourceTest.cs dio.LegacyEditor.Razor.Test/Completion/VisualStudioDescriptionFactoryTest.cs dio.�� dio.LegacyEditor.Razor.Test/Completion/RazorDirectiveCompletionSourceProviderTest.cs dio.LegacyEditor.Razor.Test/Completion/LegacyTagHelperCompletionServiceTest.cs (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Enhancement: Blazor / Razor infer @ref definitions from razor language.</issue_title>
<issue_description>When defining a reference @ref to a html element or razor component you now need to define it double:

<Component @ref="myComponent">Hello Blazor!</Component>

@code {
    private Component myComponent;

    void DoSomething() => myComponent.ShowMessage();
}

You now need to define the type and name twice, both in the razor language and in de code block. Making refactoring / code maintenance more difficult when eg the Component is renamed. It would be a great improvement if the reference in the code block doesnt need to defined aswell. But that it's inferred from the razor language.

Same as for example in Wpf. There your can give a Name to a element and then it's available in the code-behind

So the final solution will be then be:

<Component @ref="myComponent">Hello Blazor!</Component>

@code {
    void DoSomething() => myComponent.ShowMessage();
}
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@</author><body>
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our [Triage Process](https://github.com/dotnet/aspnetcore/blob/master/docs/TriageProcess.md).</body></comment_new>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 16, 2026 00:29
…eeds debugging

Co-authored-by: dbreshears <3432571+dbreshears@users.noreply.github.com>
…nsertion issue

Co-authored-by: dbreshears <3432571+dbreshears@users.noreply.github.com>
Copilot AI changed the title [WIP] Enhance Blazor/Razor to infer @ref definitions from razor language Add automatic field generation for @ref attributes in Blazor components Jan 16, 2026
Copilot AI requested a review from dbreshears January 16, 2026 00:35
@dbreshears dbreshears assigned chsienki and unassigned dbreshears Jan 16, 2026
@dbreshears dbreshears requested a review from chsienki January 16, 2026 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: Blazor / Razor infer @ref definitions from razor language.

3 participants