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

Passing object as parameter to component fails with implicit operator #5632

Closed
tidyui opened this issue Nov 7, 2018 · 3 comments
Closed
Labels
affected-very-few This issue impacts very few customers area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-blazor-component-model Any feature that affects the component model for Blazor (Parameters, Rendering, Lifecycle, etc) ✔️ Resolution: Duplicate Resolved as a duplicate of another issue severity-major This label is used by an internal tool Status: Resolved
Milestone

Comments

@tidyui
Copy link

tidyui commented Nov 7, 2018

Consider the following code:

namespace MyModels
{
    public interface IField 
    {
        Guid Id { get; set; }
    }

    public class StringField : IField
    {
        public Guid Id { get; set; }
        public string Value { get; set; }

        public static implicit operator string(StringField field)
        {
            return field.Value;
        }
    }
}

I have a component for rendering a StringField in the UI that simplified looks like this:

<input type="text" bind="@Data.Value" />

@functions {
    [Parameter]
    private MyModels.StringField Data { get; set; }
}

In my main page I operate on a collection of IField objects. Trying to invoke the component like this fails as runtime. This has worked fine for all previous components that didn't have the implicit operator.

<StringField Data="@((MyModels.StringField)Field)"></StringField>

However changing the component to this and passing the param as an IField works just fine but is a bit messy.

<input type="text" bind="@(((MyModels.StringField)Data).Value)" />

@functions {
    [Parameter]
    private MyModels.IField Data { get; set; }
}

It looks like the implicit operator converts the param before trying to set it in runtime making the app break.

@tidyui
Copy link
Author

tidyui commented Nov 7, 2018

I've created a test repository here https://github.com/tidyui/BlazorBindingTest that shows the three scenarios:

  1. StringField failing with implicit op /fieldfails
  2. StringField works without implicit op /fieldworks
  3. StringField works with implicit op when passed as interface /fieldbyinterface

@danroth27
Copy link
Member

@SteveSandersonMS This seems to happen pretty deep in how parameters get handled.

@aspnet-hello aspnet-hello transferred this issue from dotnet/blazor Dec 17, 2018
@aspnet-hello aspnet-hello added this to the Backlog milestone Dec 17, 2018
@aspnet-hello aspnet-hello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. area-blazor Includes: Blazor, Razor Components labels Dec 17, 2018
@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@SteveSandersonMS SteveSandersonMS added affected-very-few This issue impacts very few customers severity-major This label is used by an internal tool labels Oct 14, 2020 — with ASP.NET Core Issue Ranking
@javiercn javiercn added the feature-blazor-component-model Any feature that affects the component model for Blazor (Parameters, Rendering, Lifecycle, etc) label Apr 19, 2021
@mkArtakMSFT
Copy link
Member

Closing this as a dupe of #18042

@mkArtakMSFT mkArtakMSFT closed this as not planned Won't fix, can't repro, duplicate, stale Oct 19, 2022
@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Oct 19, 2022
@ghost ghost added the Status: Resolved label Oct 19, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Nov 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-very-few This issue impacts very few customers area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-blazor-component-model Any feature that affects the component model for Blazor (Parameters, Rendering, Lifecycle, etc) ✔️ Resolution: Duplicate Resolved as a duplicate of another issue severity-major This label is used by an internal tool Status: Resolved
Projects
None yet
Development

No branches or pull requests

6 participants