Skip to content

Generate Blazor control class from Roslyn generator #64102

@GLuca74

Description

@GLuca74

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I’m writing my first Roslyn-based code generator. The goal of the generator is, when certain conditions are met, to write a Blazor component and a related class, and make them available to the user. Currently, the component is generated, and Visual Studio’s editor recognizes it without issues, but at build time the control is not recognized. The Output window shows:

warning RZ10012: Found markup element with unexpected name [...]. If this is intended to be a component, add a @using directive for its namespace.

I’ve prepared a solution to reproduce the issue, available at https://github.com/GLuca74/TestRoslyn.

The solution consists of:

  • Default templates for .NET MAUI Blazor Hybrid and Web App (TestRoslyn, TestRoslyn.Shared, TestRoslyn.Web, TestRoslyn.Web.Client)

  • TestRoslyn.RCL, which references TestRoslyn.Generator and contains the base control (TestComponent.razor) from which the generated control should inherit:

@typeparam T

<h3>TestComponent</h3>
<span>@typeof(T).FullName</span>

@code {

}
  • TestRoslyn.Generator
    This project contains the generator that will produce the control to be made available to the user.
    In this project, the MyGenerator class works as follows: if it finds a class in the consumer project whose name starts with TestClass, it generates a component and a corresponding class based on the rest of the name. For example, in TestRoslyn.Shared there is a class TestClassA. In this case, the generator produces:
namespace TestRoslyn.Shared
{
    public class AComponent : TestRoslyn.RCL.TestComponent<AClass>
    {
    }

    public class AClass
    {
    }
}

In the Counter page, I added the component:

<AComponent/>

Image

Visual Studio’s editor recognizes the control and does not report any issues. However, when I run the application, I get the warning:

warning RZ10012: Found markup element with unexpected name 'AComponent'. If this is intended to be a component, add a @using directive for its namespace.

The app still runs, and inspecting with debugging tools, the control is rendered in both the WebApp and the MAUI app just as:

<acomponent></acomponent>

To verify that the base component works, I also added the class ManualComponent in the TestRoslyn.Shared project:

public class ManualComponent : TestRoslyn.RCL.TestComponent<AClass>
{
}

ManualComponent renders without any issues.

So the problem seems to be that the generated component AComponent is not visible to the compiler at build time. Is there a way to overcome this and make AComponent available to the compiler?

Thanks!

Expected Behavior

The generated component AComponent should be fully recognized by the compiler and the Blazor runtime, just like a manually created component.

Steps To Reproduce

https://github.com/GLuca74/TestRoslyn

Exceptions (if any)

No response

.NET Version

9.0.304

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions