C# 如何在.Net MVC中仅为类中的真布尔值打印标签?
我有一个名为AccessRole的类C# 如何在.Net MVC中仅为类中的真布尔值打印标签?,c#,asp.net,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc 4,Razor,我有一个名为AccessRole的类 namespace InnovatorYoung.Models { public class AccessRole { public bool SuperAdmin { get; set; } public bool Admin { get; set; } public bool Mentors { get; set; } public bool Innovators { get
namespace InnovatorYoung.Models
{
public class AccessRole
{
public bool SuperAdmin { get; set; }
public bool Admin { get; set; }
public bool Mentors { get; set; }
public bool Innovators { get; set; }
public bool AcadPartner { get; set; }
public bool Guest { get; set; }
}
}
我只需要打印布尔值为真的标签。不知道该怎么做
如果(角色超级管理员)…是的,目前我正在这样做!但为每个角色添加if语句似乎并不有效。是否有更好的方法,如“where item.value==true”或其他方法?使用基于角色的身份验证为不同的角色显示不同的视图。。
AccessRole instance = new AccessRole();
//...
Type type = typeof(AccessRole);
// Loop over properties.
foreach (PropertyInfo propertyInfo in type.GetProperties())
{
string name = propertyInfo.Name;
object value = propertyInfo.GetValue(instance, null);
if (value is bool and (bool)value)
{
// print...
}
}