Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong inferred type param when passing this to Microsoft.JSInterop.DotNetObjectRef #12446

Closed
chucker opened this issue Jul 22, 2019 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue

Comments

@chucker
Copy link

chucker commented Jul 22, 2019

Preview 6 replaces DotNetObjectRef with a generic DotNetObjectRef<T>. Instead of a constructor, the Create<T> method is now used.

When passing this from within a component, this leads to unexpected results due to Razor's parsing behavior.

To Reproduce

Steps to reproduce the behavior:

  1. Using ASP.NET Core 3.0 Preview 6 and Visual Studio 2019 16.2 Preview 4
  2. Create a new Blazor component MySampleComponent.razor
  3. Place this code inside its @code section:
    private DotNetObjectRef<MySampleComponent> _Instance;

    protected override async Task OnAfterRenderAsync()
    {
        _Instance = DotNetObjectRef.Create(this);
    }
  1. See error

Expected behavior

This should compile. The type parameter of Create<T> should be inferred as MySampleComponent.

Actual behavior

This doesn't work, because Razor internally renames your class to __generated__MySampleComponent. Consequently, the Error List will show:

CS0029 Cannot implicitly convert type Microsoft.JSInterop.DotNetObjectRef<__generated__MySampleComponent> to Microsoft.JSInterop.DotNetObjectRef<MySampleComponent>

If you explicitly specify the type parameter, the error changes to CS1503.

Workaround

You can declare the variable with the generated class name:

    private DotNetObjectRef<__generated__MySampleComponent> _Instance;

    protected override async Task OnAfterRenderAsync()
    {
        _Instance = DotNetObjectRef.Create(this);
    }

However, this is presumably not the intended behavior.

@chucker
Copy link
Author

chucker commented Jul 22, 2019

I filed this a bit prematurely. The above error messages are real. However, when actually building the project, you need to go with the original code.

So, while this will show an error during Razor compilation:

    private DotNetObjectRef<MySampleComponent> _Instance;

    protected override async Task OnAfterRenderAsync()
    {
        _Instance = DotNetObjectRef.Create(this);
    }

, it is in fact the correct syntax for building the project.

@Pilchie Pilchie added the area-blazor Includes: Blazor, Razor Components label Jul 22, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @chucker.
This will be resolved as part of #4065

@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Jul 23, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants