> 文章列表 > .net特性(个人笔记)

.net特性(个人笔记)

.net特性(个人笔记)

dui前言

个人理解的特性。对标 简单的AOP编程

就是在对于的 类、方法、属性等上面声明一个标签--->然后利用反射的知识对标签进行解析-->进行某些特殊处理的判断。 

实现过程

类/方法声明特性

    /// <summary>/// 自定义Custom特性/// </summary>[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]public class CustomAttribute : Attribute{/// <summary>/// 无参构造函数/// </summary>public CustomAttribute(){}/// <summary>/// 有参构造函数/// </summary>/// <param name="id"></param>public CustomAttribute(string description){this.Description = description;}/// <summary>/// 属性/// </summary>public string Description { get; set; }/// <summary>/// 字段/// </summary>public string Remark = null;public void Show(){Console.WriteLine("This Is CustomAttribute");}}

对类使用特性 

[Obsolete("请不要使用该类了,该类已经过时了")][Custom("这是Custom自定义特性")]//, Custom, Custom(), Custom(Remark = "备注")public class Student{public int Id { get; set; }/// <summary>/// 在属性上面使用Custom特性/// </summary>[Custom("这是Name属性")]public string Name { get; set; }public string Accont { get; set; }public long QQ { get; set; }/// <summary>/// 在方法和参数上面使用Custom特性/// </summary>/// <param name="name"></param>/// <returns></returns>[Custom("这是Answer方法")][return: Custom()] //方法的返回值应用特性public string Answer([Custom("这是方法参数")] string name){var ce = $"This is {name}";return ce;}}

写一个对特性的反射解析方法

    /// <summary>/// 特性扩展/// </summary>public static class AttributeExtend{/// <summary>/// 验证类/// </summary>/// <typeparam name="T"></typeparam>/// <param name="t"></param>/// <returns></returns>public static bool ValidateClass<T>(T t) {// Type type = typeof(Student); //或者使用student.GetType();Type type = t.GetType();// 找到类型上面的特性    type.IsDefined表示找类型上面的特性if (type.IsDefined(typeof(CustomAttribute), true))//检查有没有  性能高{//GetCustomAttribute 获取特性 type.GetCustomAttribute表示找到类型上面定义的特性,表示调用构造函数创建一个CustomAttribute类型的对象CustomAttribute attribute = (CustomAttribute)type.GetCustomAttribute(typeof(CustomAttribute), true);attribute.Show();}return true;}public static bool ValidateFun<T>(T t) {Type type = t.GetType();#region 获取Answer()方法上面定义的特性// 获取Answer方法MethodInfo method = type.GetMethod("Answer");if (method.IsDefined(typeof(CustomAttribute), true)){CustomAttribute attribute = (CustomAttribute)method.GetCustomAttribute(typeof(CustomAttribute), true);Console.WriteLine($"{attribute.Description}_{attribute.Remark}");attribute.Show();}#endregion#region 获取参数定义的特性ParameterInfo parameter = method.GetParameters()[0];if (parameter.IsDefined(typeof(CustomAttribute), true)){CustomAttribute attribute = (CustomAttribute)parameter.GetCustomAttribute(typeof(CustomAttribute), true);Console.WriteLine($"{attribute.Description}_{attribute.Remark}");attribute.Show();}#endregion#region 获取返回值定义的特性ParameterInfo returnParameter = method.ReturnParameter;if (returnParameter.IsDefined(typeof(CustomAttribute), true)){CustomAttribute attribute = (CustomAttribute)returnParameter.GetCustomAttribute(typeof(CustomAttribute), true);Console.WriteLine($"返回值{attribute.Description}_{attribute.Remark}");attribute.Show();}#endregionreturn true;}/// <summary>/// 验证属性/// </summary>/// <typeparam name="T"></typeparam>/// <param name="t"></param>/// <returns></returns>public static bool Validate<T>(T t){Type type = t.GetType();foreach (var prop in type.GetProperties()){if (prop.IsDefined(typeof(BaseAttribute), true)){var oValue = prop.GetValue(t, null);foreach (BaseAttribute item in prop.GetCustomAttributes(typeof(BaseAttribute), true))//获取字段所有的特性{if (!item.Validate(oValue)){return false;}}}}return true;}}

前期准备做完了,看看实现

类、方法、属性等上面声明一个标签--->然后利用反射的知识对标签进行解析-->进行某些特殊处理的判断。可以结合自己的理解想想应用场景

        static void Main(string[] args){Student student = new Student();student.Accont="";//验证类特性,触发特性里面的方法AttributeExtend.ValidateClass(student);//验证方法特性,触发特性里面的方法AttributeExtend.ValidateFun(student);//检查特性,然后触发属性里面对应的处理//验证方法特性,触发特性里面的方法var result = AttributeExtend.Validate<student>(student);Console.WriteLine("Hello World!");
}

 以上是自定义特性,.net还有很多封装特性的,感兴趣可以去查询

特性是 Fitter过滤器切面编程的底层知识,有人感兴趣的话,下篇文章详细介绍Fitter过滤器切面编程