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

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

时间:2022-12-03 21:57:18浏览次数:38  
标签:QQ return Name c# prop Validate mobileNum true public

 

1. 调用 bool bResult = ValidateAttributeExtension.Validate<StudentVip>(vip);

 

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,mobileNum,true,public
From: https://www.cnblogs.com/csj007523/p/16948846.html

相关文章

  • Ubuntu18.04安装docker
    一、安装1.更新源sudoapt-getupdate2.安装依赖:sudoapt-getinstallapt-transport-httpsca-certificatescurlgnupg2software-properties-common3.信任Do......
  • 【五期邹昱夫】USENIX Security(USENIX Security'21)Systematic Evaluation of Privacy
    The30thUSENIXSecuritySymposium.  本文贡献主要有两部分,一是提出一种基于修改预测熵的新成员推理攻击方法。二是定义了一个称为隐私风险分数的指标,用来估计每......
  • 安恒杯 ctf 2022
    前言:安恒杯ctf2022初赛,简单记录下,时间有限就做了3道web和2道reverseweb-测一测签到题,直接访问index.php.bak即可拿到flagweb-lander考点是jwt配合spel表达式注入,用......
  • Dockerfile自定义镜像
    常见的镜像在DockerHub就能找到,但是我们自己写的项目就必须自己构建镜像了。而要自定义镜像,就必须先了解镜像的结构才行。1.镜像结构镜像是将应用程序及其需要的系统函......
  • 多分类f1-score,Micro-F1和Macro-F1
    研究生开学以后不怎么写博客了,其实应该坚持写的。分类模型的指标:f1-score,auc,roc曲线,precision,specificity,sensitivity,recall,accuracyconfusionmatrix混淆矩阵 ......
  • Docker Compose 知识点
    docker-compose.yml中文件格式版本version,与DockerEngine兼容性:https://docs.docker.com/compose/compose-file/compose-versioning/#compatibility-matrix......
  • win10 中 anaconda3 安装 pytorch 教程
    anaconda中自带python,所以不需要提前安装python。1.安装anaconda3下载链接:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/下载文件:Anaconda3-2021.11-Windo......
  • ( Java 和 C++ 还是有差别)卑微地向API低下了头,但是反转字符串的单词依旧写了很久 学
    344.反转字符串-ezclassSolution{publicvoidreverseString(char[]s){intleft=0,right=s.length-1;chartmp;while(l......
  • bouncycastle安装配置
    bouncycastle简介bouncycastle(轻量级密码术包)是一种用于Java平台的开放源码的轻量级密码术包;它支持大量的密码术算法,并提供JCE1.2.1的实现。bouncycastle安装环境......
  • Blazor Server完美实现Cookie Authorization and Authentication
    Blazorserver-sideapplication用Microsoft.AspNetCore.Identity.EntityFrameworkCore实现Authorization和Authentication完整教程。本方案只适用于BlazorServer-Siz......