1. 继承ValidationAttribute写特性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
namespace ConsoleApplication3
{
[AttributeUsage(AttributeTargets.Property)]
public class RangeAttribute : ValidationAttribute
{
public double Min { set; get; }
public double Max { set; get; }
public string ErrorMessage{set;get;}
public RangeAttribute()
: base()
{
}
public RangeAttribute(string errorMessage, double min, double max)
: base(errorMessage)
{
this.ErrorMessage = errorMessage;
this.Min = min;
this.Max = max;
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name, ErrorMessage);
}
public override bool IsValid(object value)
{
bool flag = true;
double validValue = Convert.ToDouble(value);
if (validValue < Min || validValue > Max)
{
flag = false;
}
return flag;
}
}
}
2. 写测试实体Student
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication3
{
public class Student
{
public int ID { set; get; }
public string Name { set; get; }
[Range("aaa",1,1000)]
public string Num { set; get; }
}
}
3. 写验证接口IValidator<T>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
public interface IValidator<T> where T:class
{
bool IsValid(T t);
}
}
4.实现验证接口Validator<T>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication3
{
public class Validator<T>:IValidator<T> where T:class
{
public bool IsValid(T t)
{
if (t == null)
{
throw new ArgumentNullException("t");
}
Type type = t.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
//获取验证特性
object[] validateContent = property.GetCustomAttributes(typeof(ValidationAttribute), true);
if (validateContent != null)
{
//获取属性的值
object value = property.GetValue(t, null);
foreach (ValidationAttribute validateAttribute in validateContent)
{
if(!validateAttribute.IsValid(value))
{
return false;
}
}
}
}
return true;
}
}
}
5. 测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.ID = 1;
student.Name = "";
student.Num = "sdfsdfsd";
Validator<Student> validator = new Validator<Student>();
Console.WriteLine(validator.IsValid(student).ToString());
Console.Write("start");
Console.ReadKey();
}
}
}
分享到:
相关推荐
C# 特性 Attribute
public class JWAttribute : Attribute { public string param1 { get; set; } public string param2 { get; set; } public string paramDefault = "paramDefault"; } [JW(param1 = "JW-A")] public class A { ...
3、例子中实现了一个自定义的Attribute:DisplayNameAttribute和一个自定一个TypeConverter:EnumConverter,以支持显示的值与实际选中的值可以不同并一一对应。 注意,各个属性均是自己定义的一个枚举类型...
【内容概要】:在C#中通过学习使用Attribute,实现一个简单的ORM框架。里面主要有两部分的内容,学习使用Attribute获得设定值;学习通过反射获取属性的值 【适应人群】:初级工程师。但需要对反射、Attribute有一定...
【涉及的知识点】:自定义Attribute、C#中的反射知识 【使用人群】:初级工程师,适合刚开始学习的人群 【使用场景及目标】:学习如何使用反射获取特性值,为下一步学习做好准备 【其他说明】:暂无
glsl里自定义attribute供VS引用
8月24号做的小例子,创建Attribute子类,并且获得元数据值
使用 ASP.NET Core 3.x 构建 RESTful Web API(2020-2-7更新) P28_输入验证_自定义Attribute_Routine.Api2020_2_13.rar P28 输入验证:自定义Attrbute
c#中的特性(attribute)+反射的一个例子
预定义特性 AttributeUsage 描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型
Attribute在NET中的应用 讲解在NET中如何自定义自己的特性(Attribute)如何使用
Attribute(特性)的概念不在此赘述了,相信有点.NET基础的开发人员都明白,用过Attribute的人也不在少数,毕竟很多框架都提供自定义的属性,类似于Newtonsoft.JSON中JsonProperty、JsonIgnore等 自定义特性 .NET ...
包含2篇attribute高级应用文章,以及相应的代码。 代码关键处,都有详细注释。 看过后,你会对attribute,context有更深刻的认识
.Net新手通常容易把属性(Property)跟特性(Attribute)搞混,其实这是两种不同的东西 属性指的类中封装的数据字段;而特性是对类、字段、方法和属性等元素标注的声明性信息 如下代码(Id、Name为User的属性,[Db...
c#特性(Attribute)简单示例,适合初学者
此文档详细讲解了:ASP.NET MVC5 新特性:Attribute路由的使用,非常的详细,值得学习。
MyOrm,简单的Attribute和反射 可以定义两种特性,一个数据数据库表绑定特性,名为BindTableAttribute,用于将一个对象类型绑定到一个指定表名称的数据表上;还有一个数据库字段绑定特性,名为BindFieldAttribute,...
从事asp.net工作的相关人员对Attribute并不陌生吧,本文就来为大家介绍下Attribute特性,下面有个不错的示例,喜欢的朋友感受下
AttributeError: module 'tensorflow.compat.v1' has no attribute 'contrib'的问题您具体怎么解决问题具体解决的seq_loss.py文件
python3 server.py 127.0.0.1 8888 ...AttributeError: module ‘os’ has no attribute ‘exit’ 部分代码入下: from socket import * import sys,os #实现登录 def do_login(s,user,name,addr): for i in user: i