【发布时间】:2015-01-14 15:22:09
【问题描述】:
我正在尝试使用简单的布尔结果制作自定义数据注释属性,如果属性具有数据注释,则执行操作。我的目标是创建一个包含所有具有过滤器数据注释的属性的数组,以便稍后根据该信息创建一个 DropDownListFor。
public class Foo
{
public string property1 { get; set; }
[Filter]
public string property2 { get; set; }
}
然后在代码的某处:
IList<PropertyInfo> properties = typeof(Foo).GetProperties().ToList();
ArrayList propertiesThatCanBeFiltered = new ArrayList();
foreach (var propertyInfo in properties)
{
if (propertyInfo.Attributes("Filter").Exists)
{
propertiesThatCanBeFiltered.Add(propertyInfo.Name);
}
}
【问题讨论】:
-
这就是 Actionfilters 发挥作用的地方。 msdn.microsoft.com/en-us/library/dd410056(v=vs.100).aspx
-
你能试试像 ((DisplayAttribute)(myFooObject.GetType().GetProperty("property2 ").GetCustomAttributes(typeof(DisplayAttribute),true)[0]));
标签: asp.net asp.net-mvc entity-framework data-annotations