question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

17.6 CS0029 Cannot implicitly convert type 'Microsoft.AspNetCore.Components.ElementReference' to '[class name]'

See original GitHub issue

After updating to 17.6, with inherited components we are getting CS0029 Cannot implicitly convert type 'Microsoft.AspNetCore.Components.ElementReference' to '[class name]'

This can be reproduced with the following three components:

BaseButton.razor

@inherits Button
@{
    base.BuildRenderTree(__builder);
}

@code
{
    protected virtual string Text { get; }

    protected RenderFragment TextChildContent
    {
        get
        {
            if (Text == null)
                return null;

            return @<span>@Text</span>;
        }
    }

    protected override void OnInitialized()
    {
        if (Text != null)
            ChildContent = TextChildContent;

        base.OnInitialized();
    }

    public async Task InvokeStateHasChangedAsync(bool disabled)
    {
        Disabled = disabled;

        await InvokeAsync(StateHasChanged);
    }
}

OKButton.razor

@inherits BaseButton
@{
    base.BuildRenderTree(__builder);
}

@code
{
    protected override string Text => "OK";

    protected override void OnInitialized()
    {
        Type     = ButtonType.Primary;
        HtmlType = "submit";

        base.OnInitialized();
    }
}

ButtonTest.razor

<OKButton   @ref = "_object" />
<BaseButton @ref = "_baseButton" />
<OKButton   @ref = "_okButton" /> @*CS0029 compile error*@

@code
{
    object     _object;
    BaseButton _baseButton;
    OKButton   _okButton;
}

<OKButton @ref = "_object" /> and <BaseButton @ref = "_baseButton" /> compile fine, but <OKButton @ref = "_okButton" /> produces the error, so perhaps it’s related to the second level of inheritance.

Download the three razor components here: RazorComponents.zip

In this example, the inherited Button is from AntDesign.

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Reactions:6
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
michaelcsikoscommented, May 20, 2023

What an absolute trainwreck. I don’t care if it works in the .NET 8 SDK preview; it worked just fine prior to VS 17.6. We’re using .NET 7 for projects in the real world with paying clients. This is a pretty simple scenario and I can’t understand how a regression like this creeps in.

I’ve said before that Visual Studio updates have become a joke. Two years later and the joke is still on us.

1reaction
TheCakeMonstercommented, May 30, 2023

I have managed to work around this in my own code; just posting here in case it helps anyone else.

In my scenario the code associated with the component was in a separate file, containing a partial class. I changed the partial class in the component1.razor.cs file to explicitly inherit from the class that was mentioned in the @inherit attribute in the component1.razor file and the problem went away.

If you are completely stuck then this might help.

I’ll add some sample code when I get a moment; I’ve got to go into a meeting now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error CS0029 - Blazor Component · Issue #17416
... Error CS0029: Cannot implicitly convert type 'Microsoft.AspNetCore.Components. ... component in a folder names Components under TokeHRM.
Read more >
error CS0029: Cannot implicitly convert type 'Microsoft. ...
I get this error: " error CS0029: Cannot implicitly convert type 'Microsoft.AspNetCore.Components.ElementReference' to MyComponent only when ...
Read more >
Error CS0029: Cannot implicitly convert type 'Microsoft. ...
Error CS0029: Cannot implicitly convert type 'Microsoft.AspNetCore.Components.ElementReference' after update to 17.6Closed - Fixed
Read more >
Visual Studio 2022 - 17.6 Now Available
All-in-one search makes it easy for you to quickly find a Visual Studio menu feature (Ctrl+Q) or files, types, and members in your...
Read more >
Error CS0029: Cannot implicitly convert type "string" to " ...
Resolved Error CS0029: Cannot implicitly convert type "string" to "NameScript"(A script class name).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found