自己写一个扩展方法就可以了。 public static class CustomAttributeExtensions { public static T GetCustomAttribute<T>(this PropertyInfo pi) where T : Attribute { return pi.GetCustomAttributes(inherit: true).OfType<T>().FirstOrDefault(); } }
我写的,不知道行不行,先试一下 public static List<T> CheckUploadList<T>(List<T> list) where T : class,new() { 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
另外,Dotnet4.0支持System.ComponentModel.DataAnnotations,你可以直接使用(不建议自己重复): 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 { 省略 }
没有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
{
省略
}