jeudi 18 mars 2021

Is there a way to set a generic type to a Blazor component at runtime?

<GenericInputFor> provides markup based on the argument it receives and its type, it behaves fine with compile time types, but when I try to iterate through some class properties with reflection it says that the type cannot be inferred :

 @foreach (var property in typeof(SomeClass).GetProperties())
        {
           <GenericInputFor Model="property.GetValue(in2,null)" ModelChanged="@((ChangeEventArgs __e) => property.SetValue(in2, __e.Value))"></GenericInputFor>
        }

@code {
    public SomeClass in2 { get; set; }

    protected override void OnInitialized()
    {
        in2 = new SomeClass();
        base.OnInitialized();
    }
}

I have seen multiple awesome solutions like Setting generic type at runtime and Creating a Generic type instance with a variable containing the Type but I couldn't apply it to blazor components.





Aucun commentaire:

Enregistrer un commentaire