c# 抽象类+特性 验证QQ,Mobile,Name的通用写法
1. 调用 bool bResult = ValidateAttributeExtension.Validate<StudentVip>(vip);
[Custom]
public class StudentVip : Student
{
[Custom]
public string Description;
//[Custom]
[QQAttribute(_MinLenth = 5, _MaxLenth = 12)]
public string QQ { [Custom(456, "Ricahrd")][Custom(567, "Ricahrd1")][Custom(789, "Ricahrd2")] get; set; }
[MobileNumAtrribute(11)]
public long MobileNum { get; set; }
}
2.特性的额外方法:验证功能:
public class ValidateAttributeExtension
{
public static bool Validate<T>(T t)
{
Type type = t.GetType();
foreach (PropertyInfo prop in type.GetProperties())
{
///验证手机号长度
//if (prop.IsDefined(typeof(MobileNumAtrribute), true))
//{
// object oValue = prop.GetValue(t);
// MobileNumAtrribute atrribute = prop.GetCustomAttribute<MobileNumAtrribute>(true);
// if (!atrribute.Validate(oValue))
// {
// return false;
// }
//}
/////这就是验证QQ
//if (prop.IsDefined(typeof(QQAttribute), true))
//{
// object oValue = prop.GetValue(t);
// QQAttribute atrribute = prop.GetCustomAttribute<QQAttribute>(true);
// if (!atrribute.Validate(oValue))
// {
// return false;
// }
//}
//如果后续还需要再加一个验证呢?那岂不是 又要修改代码?
//这样做是坑,抽象~~
if (prop.IsDefined(typeof(AbstractValidateAttribute), true))
{
object oValue = prop.GetValue(t);
AbstractValidateAttribute atrribute = prop.GetCustomAttribute<AbstractValidateAttribute>(true);
if (!atrribute.Validate(oValue))
{
return false;
}
}
}
return true;
}
}
3.验证QQ的特性继承抽象类+验证QQ的长度
public class QQAttribute: AbstractValidateAttribute
{
public int _MinLenth;
public int _MaxLenth;
public override bool Validate(object mobileNum)
{
return mobileNum != null && mobileNum.ToString().Length >= _MinLenth && mobileNum.ToString().Length <= _MaxLenth;
//if (mobileNum!=null&& mobileNum.ToString().Length>= _MinLenth && mobileNum.ToString().Length<= _MaxLenth)
//{
// return true;
//}
//return false;
}
}
4.抽象类继承特性
public abstract class AbstractValidateAttribute : Attribute
{
public abstract bool Validate(object value);
}
----------------------------------------------------------- 特性应用 -status 枚举 enum------------------------------------------
0.调用
var normal = UserStuta.Normal;
var frozen = UserStuta.Frozen;
string strnormal = RemarkExtension.GetRemark(normal); //获取枚举描述
string strfrozen = RemarkExtension.GetRemark(frozen);
1.remark 扩展
public static class RemarkExtension
{
public static string GetRemark(this Enum @enum) //扩展方法
{
Type type = @enum.GetType();
FieldInfo? fileInfo = type.GetField(@enum.ToString());
if (fileInfo != null)
{
if (fileInfo.IsDefined(typeof(RemarkAttribute), true))
{
RemarkAttribute remarkAttribute = (RemarkAttribute)fileInfo.GetCustomAttribute(typeof(RemarkAttribute), true);
return remarkAttribute.Remark;
}
}
return @enum.ToString();
}
}
2.remark特性
[AttributeUsage(AttributeTargets.Field)]
public class RemarkAttribute : Attribute
{
public string Remark { get; private set; }
public RemarkAttribute(string remark)
{
this.Remark = remark;
}
}
相关文章
- [C#] 处理 Json
- C#订阅与发布标准实现 visual studio code .net 开发 设计模式之☞策略模式 C#字符串转二进制、二进制转字符串 c# 接口的协变和逆变 c# 使用迭代器来创建可枚举类型 博客园首页新随笔联系订阅管理 随笔 - 117 文章 - 0 评论 - 57 c# 创建,加载,修改XML文档
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- C#不用union,而是有更好的方式实现 .net自定义错误页面实现 .net自定义错误页面实现升级篇 .net捕捉全局未处理异常的3种方式 一款很不错的FLASH时种插件 关于c#中委托使用小结 WEB网站常见受攻击方式及解决办法 判断URL是否存在 提升高并发量服务器性能解决思路
- 一个简单的QQ隐藏图生成算法 通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- C# IL DASM 使用-破解c#软件方法
- Word控件Spire.Doc 【页眉页脚】教程(10): 锁定标题以防止在 C# 中编辑 word 文档
- C#数据之DataTable
- C#【高级篇】.NET平台调用Win32 API
- C#,聚会数(相遇数,Rencontres Number)的算法与源代码
- C#,打印漂亮杨辉三角形(帕斯卡三角形)的源代码
- C#-文件只读时进行拷贝或删除报错, 对路径不允许访问
- C#迭代器
- C#引用多个接口案例教程
- C# 读文件批量修改文件夹中的数据
- 一款开源且功能强大的C#甘特图控件.NET Winforms Gantt Chart Control
- C#多线程问题(从不同步的代码块中调用了对象同步方法。)
- C# Color值转换为unit