不能这么写
var showNameAttribute = porp.GetCustomAttribute<ShowNameAttribute>();
var showNameAttribute = porp.GetCustomAttribute<ShowNameAttribute>();
解决方案 »
- 关于带确定和取消提示框的问题!!
- asp.net页面布局问题
- asp.net中如何中止页面的运行而不刷新页面
- 一个存储过程 请这会儿不忙的人看看有什么毛病
- 请问如何在asp.net中做二级联动的 DropDownList
- 怎么样使鼠标移动到DataGrid中某一行时,变成手的形状,并且显示“点击查看”?在线等!
- DataAdapter.DeleteCommand用法的疑惑!!!
- 麻烦诸位高手帮我看看这段简单的源代码。
- 如何在windows xp professional上安装sqlserver 2000 enterprise?
- 谁能推荐一本做控件的好书?
- 关于webapi的token验证,缓存方式
- 求帮助!
没有System.Runtime,取属性的值也取不了
public static class CustomAttributeExtensions
{
public static T GetCustomAttribute<T>(this PropertyInfo pi) where T : Attribute
{
return pi.GetCustomAttributes(inherit: true).OfType<T>().FirstOrDefault();
}
}
{
T t = new T();
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (var item in list)
{
//遍历该对象的所有属性
foreach (PropertyInfo pi in propertys)
{
FieldCheck data = (FieldCheck)pi.GetValue(item,null);
data.Checked = true;
data.CheckResult = true;
object o = pi.GetType().GetCustomAttributes(typeof(NonNullAttribute), true).FirstOrDefault();
object[] oArr = pi.GetType().GetCustomAttributes(typeof(DataVerifyAttribute), true);
if (data.CheckResult)
{
data.FieldValue = data.FieldText;
}
pi.SetValue(item, data, null);
}
} return list;
} public static FieldCheck CheckData(FieldCheck data, params object[] parameters)
{
if (parameters != null)
{
foreach (var item in parameters)
{
DataVerifyAttribute dataVerifyAttribute = (DataVerifyAttribute)item;
data = dataVerifyAttribute.DataVerify(data);
}
}
return data;
}
[NonNullAttribute]
[LengthMaxAttribute(4)]
public FieldCheck NCMP { get; set; }
object o = pi.GetType().GetCustomAttributes(typeof(NonNullAttribute), true).FirstOrDefault();
object[] oArr = pi.GetType().GetCustomAttributes(typeof(DataVerifyAttribute), true);
object o = pi.GetType()
这里不该GetType
RequiredAttribute
StringLengthAttribute
...
using System;
using System.ComponentModel.DataAnnotations; // 要添加引用
using System.Reflection;class Program
{
static void Main(string[] args)
{
var my = new MyData() {Name = "12345678901234567890" }; // 示范自己检查特性
var myMaxLengthAttr = my.GetType().GetProperty("Name").GetCustomAttribute<MyMaxLengthAttribute>();
Console.WriteLine(myMaxLengthAttr.MaxLength); try
{
// 示范ValidationAttribute
Validator.ValidateObject(my, new ValidationContext(my, null, null), validateAllProperties: true);
}
catch(ValidationException ve)
{
Console.WriteLine(ve.Message);
}
}
}public class MyData
{
[Required, StringLength(maximumLength: 8)]
public string Id { get; set; } [MyMaxLength(maxLength: 16)]
public string Name { get; set; }
}public class MyMaxLengthAttribute : ValidationAttribute
{
public int MaxLength { get; private set; }
public MyMaxLengthAttribute(int maxLength)
{
MaxLength = maxLength;
}
public override bool IsValid(object value)
{
if (value is string) return (value as string).Length <= MaxLength;
if (value is Array) return (value as Array).Length <= MaxLength;
return false;
}
}public static class CustomAttributeExtensions
{
public static T GetCustomAttribute<T>(this MemberInfo pi) where T : Attribute
{
return (T)Attribute.GetCustomAttribute(pi, typeof(T));
}
}
object o = pi.GetType()
这里不该GetType
改了一下是可以了,不过还有一点小问题,循环是倒的
比如应该先判断是否为空,再判断长度,以及其它。。
然后循环时先判断的长度,第二次循环的才是是否为空
IEnumerable<DataVerifyAttribute> attributeList = pi.GetCustomAttributes(true).OfType<DataVerifyAttribute>();
foreach (var item in attributeList)
{
data = item.DataVerify(data);
}
[NonNullAttribute]
[LengthMaxAttribute(4)]
public FieldCheck NCMP { get; set; }
public abstract class DataVerifyAttribute : Attribute
{
public abstract FieldCheck DataVerify(FieldCheck data);
}
public class NonNullAttribute : DataVerifyAttribute
{
省略
}
public class LengthMaxAttribute: DataVerifyAttribute
{
省略
}