我需要在Blazor中迭代TValue类型属性。
组件标签为MYComponent。
<select>
@foreach (var item in this.Value(TValue))
{
<option selected value=@item></option>
}
</select>
@code {
public class MYComponent {
public TValue Value {get;set;}
private string[] MyValue = new string[] {"Value1", "Value2"};
}
}
答案 0 :(得分:1)
如果我很了解您的问题,可以通过反射来解决:
@foreach (var item in GetProperties(TValue))
{
<option selected value=@item></option>
}
</select>
@code {
public class MYComponent
{
public TValue Value {get;set;}
private string[] MyValue = new string[] {"Value1", "Value2"};
public IEnumerable<string> GetProperties()
{
return typeof(TValue).GetProperties().Select(p => p.Name);
}
}
}