如何在Blazor中迭代TValue类型属性

时间:2020-02-18 08:58:52

标签: blazor blazor-server-side

我需要在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"};
}
}

1 个答案:

答案 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);
        }
    }
}