What is attribute and how to use it?
Preface
Yes, you read that right, this is a pure English headline article. Skin it.
What are characteristics?
- Attribute is actually a class, which is directly/indirectly inherited from Attribute.
- By convention, it ends with Attribut, and is wrapped with [] when marking, and the Attribut at the end can be omitted;
what does attribute can do?
那么特性可以做什么呢?
1.特性:可以增加额外信息,增加额外功能;说人话就是在类的上边用[]括起来,里边写一个类。然后会在创建类对象的时候把特性的类也实例。
Tips:特性不可直接调用。可利用反射技术来调用。也许还有其它,我没有学到。
2.特性的一个小例子可实现程序的高扩展与统一的性质。只需要把属特性的内容修改。就可实现功能。
So how do you use features so awesome?
Don't talk about Sao; go to the teacher code.
Why not upload my code? Because my code is quite awesome (usually I finish writing the blog first and sort out the knowledge before doing code exercises.)
1 main instance call code
//特性增加额外功能;
StudentVip vip = new StudentVip() {
Id = 10000, Name = "lny", QQ = "773984283", MobileNum = 12345678978 };
//在做各位,QQ位数是多少? 据我所致:QQ位数是以10000开头;
//如果说我们在新增数据到数据库中去的时候,QQ的长度是有限制的 位数:5位数<=QQ<=12位数
//if (!string.IsNullOrWhiteSpace(vip.QQ)&& vip.QQ.Length>=5 && vip.QQ.Length<=12)
//{
//}
手机号是11位
//if (true)
//{
//}
bool bResult = ValidateAttributeExtension.Validate<StudentVip>(vip);
上边代码我进行下讲解。StudentVip这个类,被实例化一下,里边有很多信息。qq呀,手机号呀,如果这些数据直接保存肯定不行。咱们得校验下是否合法。那么调用了下ValidateAttributeExtension类中的静态方法Validate。下边上ValidateAttributeExtension类。
2 Encapsulation class called
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;
}
}
}
return true;
}
}
上边实现了传来的StudentVip的手机号的验证。
- Get the type of the passed class first.
- Traversing the attributes is the parameter of {get; set; }.
- Determine whether the current attribute contains the characteristics of the mobile phone number verification class.
- Get the current mobile phone number value.
- Get the instance of the feature, and then call the Validate method.
3. The MobileNumAtrribute class.
public class MobileNumAtrribute : AbstractValidateAttribute
{
public int Count {
get; private set; }
public MobileNumAtrribute(int count)
{
if (count<=0)
{
throw new Exception("手机号长度不可能为负数");
}
Count = count;
}
public override bool Validate(object mobileNum)
{
return mobileNum != null && mobileNum.ToString().Length == Count;
//if (mobileNum != null && mobileNum.ToString().Length == Count)
//{
// return true;
//}
//else
//{
// return false;
//}
}
}
MobileNumAtrribute继承自AbstractValidateAttribute的原因是因为AbstractValidateAttribute为抽象类,这样可以用一个实例来调用他的子集,说到这你是不是能想到2应该替换成什么样呢?
//如果后续还需要再加一个验证呢?那岂不是 又要修改代码?
//这样做是坑,抽象~~
if (prop.IsDefined(typeof(AbstractValidateAttribute), true))
{
object oValue = prop.GetValue(t);
AbstractValidateAttribute atrribute = prop.GetCustomAttribute<AbstractValidateAttribute>(true);
if (!atrribute.Validate(oValue))
{
return false;
}
}
Next for the display of feature calls
public class StudentVip : Student
{
[QQAttribute(_MinLenth = 5, _MaxLenth = 12)]
public string QQ {
get; set; }
[MobileNumAtrribute(11)]
public long MobileNum {
get; set; }
}
1. [MobileNumAtrribute(11)]这块就是给MobileNum属性增加了一个特性。调用类的有参构造函数,给Count赋值11位
2. 接下来就可以在使用的时候调用Validate来验证长度是否合法。