我在viewmodelBase类中具有以下帮助方法,它由其他视图模型继承:
public string GetEnumName<T>(Enum value) { Type enumType = typeof(T); var enumValue = Enum.GetName(enumType,value); MemberInfo member = enumType.GetMember(enumValue)[0]; var attrs = member.GetCustomAttributes(typeof(displayAttribute),false); var outString = ((displayAttribute)attrs[0]).Name; if (((displayAttribute)attrs[0]).ResourceType != null) { outString = ((displayAttribute)attrs[0]).GetName(); } return outString; }
我就这样从这个角度来看:
<p> @{var rel = Model.GetEnumdisplayName<Enums.wheteverEnum>(Model.wheteverEnum); } @rel </p>
问题是 – 我可以使用这种方法,所以我不必告诉它枚举的类型?基本上我想对所有的枚举做这个:
@ Model.GetEnumdisplayName(Model.wheteverEnum)
没有类型,没有T,不需要添加对枚举命名空间在视图…中的引用
可能?
解决方法
您可以简单地删除类型参数并使其成为扩展方法。
public static string displayName(this Enum value) { Type enumType = value.GetType(); var enumValue = Enum.GetName(enumType,value); MemberInfo member = enumType.GetMember(enumValue)[0]; var attrs = member.GetCustomAttributes(typeof(displayAttribute),false); var outString = ((displayAttribute)attrs[0]).Name; if (((displayAttribute)attrs[0]).ResourceType != null) { outString = ((displayAttribute)attrs[0]).GetName(); } return outString; } @Model.wheteverEnum.displayName()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。