首页 > 编程语言 >c# 抽象类+特性 验证QQ,Mobile,Name的通用写法

c# 抽象类+特性 验证QQ,Mobile,Name的通用写法

时间:2022-12-10 16:05:18浏览次数:49  
标签:QQ return Name c# prop Validate true public string

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;
}
}

标签:QQ,return,Name,c#,prop,Validate,true,public,string
From: https://blog.51cto.com/u_15310950/5927710

相关文章

  • Oracle误删除数据的恢复
    Oracle误删除数据的恢复在平时操作数据库时,难免会误删数据,或者表格,这时候不用慌张,按照如下步骤进行恢复:删除的操作有三种:DELETE和TRUNCATE只删除数据,DROP则删除整个表(结构......
  • Tcpdump
    网络数据包截获分析工具。支持针对网络层、协议、主机、网络或端口的过滤。并提供and、or、not等逻辑语句帮助去除无用的信息。不指定任何参数监听第一块网卡上经过的数据包......
  • 图文解说ChinaCock FMX组件CCListBox
    随着Delphi11发版以来,其ListBox的效率已经不可同日而语,再加之Skia的支持,使用ListBox显示大量数据,效率已经不问题。在此情况下,ChinaCock作者继承ListBox,为我们封装了新的组......
  • SpringBoot3.x中spring.factories功能被移除的解决方案
    背景笔者所在项目组在搭建一个全新项目的时候选用了​​SpringBoot3.x​​​,项目中应用了很多​​SpringBoot2.x​​​时代相关的第三方组件例如​​baomidou​​​出品的​......
  • post表单数据格式完全解析multipart/form-data(C#实现)
    post表单数据格式完全解析multipart/form-data参数说明内容boundaryboundary一个字符串,用以分隔不同的参数;stringboundary=Guid.NewGuid().ToString()......
  • 现代 CSS 之高阶图片渐隐消失术
    在过往,我们想要实现一个图片的渐隐消失。最常见的莫过于整体透明度的变化,像是这样:<divclass="img"></div>div{width:300px;height:300px;background:url(i......
  • Django之Cookies
    一cookies简述Cookie的由来大家都知道HTTP协议是无状态的。无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请......
  • 在VScode中使用Jupyter Notebook的一些技巧
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • 如何查看CPU和内存使用率
    1、远程登录后,在远程桌面的下边缘有一条横栏叫[任务栏],在[任务栏]空白的地方点击[右键]弹出[右键菜单],选择[任务管理器]或[启动任务管理器]。2、在弹出的[任务管理器]窗口,......
  • excel科学计数法转换成文本完整显示
    1、首先打开含有科学计数法显示的excel;2、在excel最上面的菜单栏点击数据,找到“分列”选项;3、点击“分列“选项,进入分列向导,采用默认选项;4、点击下一步,还是采用默认选项......