dimanche 3 janvier 2021

Creating a generic component with type variables without using RenderTreeBuilder

Currently, I build components using RenderTreeBuilder if the component has a @typeparam that is not possessed by its parent component.

i.e.

var TValue = typeof(TItem).GetProperty(SearchPropertyName).PropertyType;
var TItem = typeof(TItem);

var TComponent = typeof(MyInputSelect<,>).MakeGenericType(TItem, TValue);

__builder.OpenComponent(0, TComponent);
__builder.AddAttribute(1, "Item", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(item));
__builder.AddAttribute(2, "PropertyName", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(true));
__builder.AddAttribute(3, "Options", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck(Options));
__builder.CloseComponent();

However, maintaining the sequences is something which I would like to avoid. Is there a way to achieve the same result in the razor file alone without using the RenderTreeBuilder?

i.e.

<MyInputSelect 

  TItem="@TItem"
  TValue="@typeof(TItem).GetProperty(SearchPropertyName).PropertyType" *//Currently this will return an error because creating generic class instances requires the corresponding generic parameters provided*
  Item="@Item"
  PropertyName="@SearchPropertyName"
  Options="SelectOptions">

</MyInputSelect>

Any advices would be greatly appreciated!





Aucun commentaire:

Enregistrer un commentaire