0

<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.

Takelovski
  • 53
  • 1
  • 4

1 Answers1

0

You should try setting the TItem attribute on the element:

@foreach (var property in typeof(SomeClass).GetProperties())
{
    <GenericInputFor TItem="typeof(SomeClass)" Model="property.GetValue(in2,null)" ModelChanged="@((ChangeEventArgs __e) => property.SetValue(in2, __e.Value))"></GenericInputFor>
}
Cory Podojil
  • 776
  • 2
  • 10
  • Thank you for answering, but if you follow closely, the argument type is not ```SomeClass``` it is a type of a property belonging to ```SomeClass``` that is determined at runtime via reflection – Takelovski Mar 19 '21 at 10:33
  • Right...you can do an `@typeparam TItem` then use that reference instead of `SomeClass` – Cory Podojil Mar 19 '21 at 14:17