一:基本使用执行特性中的方法
Student student = new Student ( ) ;
Student studentVip = new StudentVip ( )
{
Id = 1 ,
Name = "热爱学习" ,
VipGroup = ".Net vip"
} ;
InvokeCenter. ManagerSudent < Student > ( studentVip) ;
[ Cutom ( 234 , "辣辣零" ) ]
[ Cutom ( 123 ) ]
[ Cutom ( 567 , Remark = ".Net VIP" , Description = ".Net VIP" ) ]
[ CutomAttributeChild ]
public class Student
{
public int Id { get ; set ; }
public string Name { get ; set ; }
public void Study ( )
{
Console. WriteLine ( $"这里是{this.Name}" ) ;
}
public string Answer ( string name)
{
return $"This is {name}" ;
}
}
[ Cutom ( 123 , Remark = ".Net vip" , Description = "vip 正在学习" ) ]
public class StudentVip : Student
{
[ Cutom ( 123 , Remark = ".Vip" , Description = ".Net vip" ) ]
public string VipGroup { get ; set ; }
[ Cutom ( 123 , Remark = ".Vip" , Description = ".Net vip" ) ]
public void Homework ( )
{
Console. WriteLine ( "Homework" ) ;
}
[ SalaryAttribute ( 1000000 ) ]
public long Salary { get ; set ; }
}
public class InvokeCenter
{
public static void ManagerSudent < T > ( T student) where T : Student
{
Console. WriteLine ( student. Id) ;
Console. WriteLine ( student. Name) ;
student. Answer ( "YZM" ) ;
Type type = student. GetType ( ) ;
if ( type. IsDefined ( typeof ( CutomAttribute) , true ) )
{
object [ ] aAttributeArry = type. GetCustomAttributes ( typeof ( CutomAttribute) , true ) ;
foreach ( CutomAttribute attribute in aAttributeArry)
{
attribute. Show ( ) ;
}
foreach ( var prop in type. GetProperties ( ) )
{
if ( prop. IsDefined ( typeof ( CutomAttribute) , true ) )
{
object [ ] aAttributeArryprop = prop. GetCustomAttributes ( typeof ( CutomAttribute) , true ) ;
foreach ( CutomAttribute attribute in aAttributeArryprop)
{
attribute. Show ( ) ;
}
}
}
foreach ( var method in type. GetMethods ( ) )
{
if ( method. IsDefined ( typeof ( CutomAttribute) , true ) )
{
object [ ] aAttributeArryMethod = method. GetCustomAttributes ( typeof ( CutomAttribute) , true ) ;
foreach ( CutomAttribute attribute in aAttributeArryMethod)
{
attribute. Show ( ) ;
}
}
}
}
}
}
[ AttributeUsage ( AttributeTargets. All, AllowMultiple = true ) ]
public class CutomAttribute : Attribute
{
public CutomAttribute ( )
{
Console. WriteLine ( "无参数构造函数" ) ;
}
public CutomAttribute ( int i)
{
Console. WriteLine ( "Int类型构造函数" ) ;
}
public CutomAttribute ( int i, string j)
{
Console. WriteLine ( "两个参数构造函数" ) ;
}
public string Remark { get ; set ; }
public string Description;
public void Show ( )
{
Console. WriteLine ( "通过反射调用特性中的方法" ) ;
}
}
public class CutomAttributeChild : CutomAttribute
{
public CutomAttributeChild ( ) : base ( 345 )
{
}
}
二:获取枚举中特性说明
string remark1 = AttributeExtend. GetRemark ( UserState. Normal) ;
string remark2 = AttributeExtend. GetRemark ( UserState. Frozen) ;
string remark3 = UserState. Deleted. GetRemark ( ) ;
public static class AttributeExtend
{
public static string GetRemark ( this Enum value )
{
Type type = value . GetType ( ) ;
var field = type. GetField ( value . ToString ( ) ) ;
if ( field. IsDefined ( typeof ( RemarkAttribute) , true ) )
{
RemarkAttribute attribute = ( RemarkAttribute) field. GetCustomAttribute ( typeof ( RemarkAttribute) , true ) ;
return attribute. Remark;
}
else
{
return value . ToString ( ) ;
}
}
}
public enum UserState
{
[ RemarkAttribute ( "正常状态" ) ]
Normal = 0 ,
[ RemarkAttribute ( "已冻结" ) ]
Frozen = 1 ,
[ RemarkAttribute ( "已删除" ) ]
Deleted = 2
}
三:特性如何增加额外方法
bool flg = AttributeExtend. Validate ( student) ;
public class AttributeExtend
{
public static bool Validate ( StudentVip student)
{
Type type = student. GetType ( ) ;
foreach ( var prop in type. GetProperties ( ) )
{
if ( prop. IsDefined ( typeof ( SalaryAttribute) , true ) )
{
object ovale = prop. GetValue ( student) ;
foreach ( SalaryAttribute attribute in prop. GetCustomAttributes ( typeof ( SalaryAttribute) , true ) )
{
return attribute. Validate ( ovale) ;
}
}
}
return false ;
}
}
[ AttributeUsage ( AttributeTargets. Property) ]
public class SalaryAttribute : AbstratValidateAttribute
{
public long _Salary { get ; set ; }
public SalaryAttribute ( long salary)
{
_Salary = salary;
}
public override bool Validate ( object oValue)
{
return oValue != null && long . TryParse ( oValue. ToString ( ) , out long lValue) && lValue < _Salary;
}
}