今天给大家讲讲.net中特性的高级使用
1.什么是特性
- 特性(Attribute)是用于在运行时传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息的声明性标签。
- 用[]来标识,在.Net 框架提供了两种类型的特性:预定义特性和自定义特性。
复制代码 2.预定义特性
- AttributeUsage
- Conditional
- obsolete
2.1 AttributeUsage
使用AttributeUsage特性,必须集成Attribute抽象类;- [AttributeUsage(AttributeTargets.Property)]//只能标记在属性上
- public class MyCustomAttribute: Attribute
- {
- }
复制代码 AttributeUsage 使用语法详细如下:- [AttributeUsage(AttributeTargets.Class |//特性只能运用于类上
- AttributeTargets.Constructor |//特性只能运用于构造函数上
- AttributeTargets.Field |//特性只能运用于字段上
- AttributeTargets.Method |//特性只能运用于方法上
- AttributeTargets.Property, //特性只能运用于属性上
- AllowMultiple = true)]//true:可以为程序元素指定有多个实例
复制代码 其中:
参数 allowmultiple(可选的)为该特性的 AllowMultiple 属性(property)提供一个布尔值。假如为 true,则该特性是多用的。默认值是 false(单用的)。
2.2 Conditional
这个预定义特性标记了一个条件方法,
它会引起方法调用的条件编译,取决于指定的值,比如 Debug 或 Trace。比方,当调试代码时显示变量的值。
规定该特性的语法如下:- public class MyTest
- {
- [Conditional("DEBUG")]
- public static void Message(string msg)
- {
- Console.WriteLine(msg);
- }
- }
- class Program
- {
- static void function1()
- {
- MyTest.Message("In Function 1.");
- function2();
- }
- static void function2()
- {
- MyTest.Message("In Function 2.");
- }
- static void Main(string[] args)
- {
- MyTest.Message("In Main function.");
- function1();
- Console.ReadLine();
- }
- }
复制代码 当上面的代码被编译和实行时,它会产生下列效果:
2.3 Obsolete 过时
这个预定义特性标记了不应被使用的步调实体。它可以让您通知编译器丢弃某个特定的目的元素。比方,当一个新方法被用在一个类中,但是您仍然想要保持类中的旧方法,您可以通过显示一个应该使用新方法,而不是旧方法的消息,来把它标记为 obsolete(过时的)。
规定该特性的语法如下:
[Obsolete(message)]
[Obsolete(message, iserror)]
其中:
参数 message,是一个字符串,描述项目为什么过时的缘故原由以及该替代使用什么。
参数 iserror,是一个布尔值。假如该值为 true,编译器应把该项目的使用当作一个错误。默认值是 false(编译器天生一个警告)。
下面的实例演示了该特性:- public class MyTest
- {
- [Obsolete("该方法已过期,你可使用xxx最新方法")]
- public static void Message(string msg)
- {
- Console.WriteLine(msg);
- }
- }
复制代码 当编译步调时会出现如下效果,通常该特性用于在方法过期上、版本变动等等
- public class MyTest
- {
- [Obsolete("该方法已经不可使用,请使用最新XXX方法",true)]
- public static void Message(string msg)
- {
- Console.WriteLine(msg);
- }
- }
复制代码 当编译步调时会出现如下效果,可导致步调无法天生
3.自定义特性
Net 框架允许创建自定义特性,用于存储声明性的信息,且可在运行时被检索。该信息根据设计标准和应用步调需要,可与任何目的元素相关。
创建并使用自定义特性包含四个步骤:
- 声明自定义特性
- 构建自定义特性
- 在目的步调元素上应用自定义特性
- 通过反射访问特性
最后一个步骤包含编写一个简单的步调来读取元数据以便查找各种符号。元数据是用于描述其他数据的数据和信息。该步调应使用反射来在运行时访问特性。
声明自动以特性
一个新的自定义特性应派生自 System.Attribute 类。比方:- /// <summary>
- /// 自定义日志打印
- /// </summary>
- [AttributeUsage(AttributeTargets.Method)]
- public class PrintLogAttribute: Attribute
- {
- private string _userName;
- private string _msg;
- public PrintLogAttribute(string userNaame, string msg)
- {
- this._userName = userNaame;
- this._msg = msg;
- Console.WriteLine($"{userNaame}于【{DateTime.Now.ToString("yyyy-MM-dd")}】{msg}");
- }
- public string GetMsg()
- {
- return $"{this._userName}于【{DateTime.Now.ToString("yyyy-MM-dd")}】{this._msg}";
- }
- }
-
- public class PrintLogTest
- {
- [PrintLog("张三","学习Attribute")]
- public void Study()
- {
- Console.WriteLine("张三在学习....");
- }
- [PrintLog("张三", "SayHello")]
- public string SayHello()
- {
- return "hello";
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- PrintLogTest test=new PrintLogTest();
- test.Study();
- Type type = test.GetType();
- var methods = type.GetMethods();//获取所有公开方法
- foreach (MemberInfo item in methods)
- {
- if (item.IsDefined(typeof(PrintLogAttribute), true))//判断该方法是否被PrintLogAttribute标记
- {
- PrintLogAttribute attribute = item.GetCustomAttribute(typeof(PrintLogAttribute)) as PrintLogAttribute;//实例化PrintLogAttribute
- var msg = attribute.GetMsg();
- Console.WriteLine($"得到标记信息:{msg}");
- }
- }
- Console.ReadKey();
- }
- }
复制代码 实行Main方法,实行如下:

4.Attribute特性的作用
使用面向抽象编程的思想进行优化,添加一个AbstractCustomAttribute抽象类,所有的校验类都继承AbstractCustomAttribute- /// <summary>
- ///
- /// </summary>
- public abstract class AbstractCustomAttribute: Attribute//继承Attribute特性类
- {
- /// <summary>
- /// 定义校验抽象方法
- /// </summary>
- /// <param name="value">需要校验的值</param>
- /// <returns></returns>
- public abstract bool Validate(object value);
- }
复制代码 RequiredAttribute、StringLengthAttribute自定义验证特性代码如下:- /// <summary>
- /// 自定义验证,验证不为空
- /// </summary>
- [AttributeUsage(AttributeTargets.Property)]
- public class RequiredAttribute : AbstractCustomAttribute
- {
- /// <summary>
- /// 重写Validate校验方法
- /// </summary>
- /// <param name="value">需要校验的参数</param>
- /// <returns></returns>
- public override bool Validate(object value)
- {
- return value != null && !string.IsNullOrWhiteSpace(value.ToString());
- }
- }
- /// <summary>
- /// 自定义验证,验证字符长度
- /// </summary>
- [AttributeUsage(AttributeTargets.Property)]
- public class StringLengthAttribute: AbstractCustomAttribute
- {
- private int _MaxLength;
- private int _MinLength;
- /// <summary>
- ///
- /// </summary>
- /// <param name="MinLength">最小长度</param>
- /// <param name="MaxLength">最大长度</param>
- public StringLengthAttribute(int MinLength,int MaxLength)
- {
- this._MaxLength = MaxLength;
- this._MinLength = MinLength;
- }
- /// <summary>
- /// 重写Validate校验方法
- /// </summary>
- /// <param name="value">需要校验的参数</param>
- /// <returns></returns>
- public override bool Validate(object value)
- {
- return value != null && value.ToString().Length >= _MinLength && value.ToString().Length <= _MaxLength;
- }
- }
复制代码 修改UserEntity实体类,添加自定义验证失败的错误信息- public static class CustomValidateExtend
- {
- /// <summary>
- /// 校验
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public static bool Validate<T>(this T entity) where T:class
- {
- Type type = entity.GetType();
- foreach (var item in type.GetProperties())
- {
- if (item.IsDefined(typeof(AbstractCustomAttribute), true))//此处是重点
- {
- //此处是重点
- foreach (AbstractCustomAttribute attribute in item.GetCustomAttributes(typeof(AbstractCustomAttribute), true))
- {
- if (attribute == null)
- {
- throw new Exception("StringLengthAttribute not instantiate");
- }
- if (!attribute.Validate(item.GetValue(entity)))
- {
- return false;
- }
- }
- }
- }
- return true;
- }
- }
复制代码 测试代码:- /// <summary>
- /// 校验结果实体类
- /// </summary>
- public class ValidateResultEntity
- {
- /// <summary>
- /// 是否校验成功
- /// </summary>
- public bool IsValidateSuccess { get; set; }
- /// <summary>
- /// 校验不通过的字段信息存储字段
- /// </summary>
- public List<FieidEntity> ValidateMessage { get; set; }
- }
- /// <summary>
- /// 字段信息
- /// </summary>
- public class FieidEntity
- {
- /// <summary>
- /// 字段名称
- /// </summary>
- public string FieidName { get; set; }
- /// <summary>
- /// 字段类型
- /// </summary>
- public string FieidType { get; set; }
- /// <summary>
- /// 验证错误时提示信息
- /// </summary>
- public string ErrorMessage { get; set; }
- }
复制代码
最终我们做到了通过特性进行校验字段数据,不再写那种繁琐又臭又长的判断代码了。以上代码还可以继承优化,还可以使用泛型缓存提高其性能。
最后介绍一波微软的模型验证,
引用【System.ComponentModel.DataAnnotations】
内里有:
[Required]
[Range]
........
详情可查看【模型验证】
使用ActionFilterAttribute过滤器我们可以进行校验操作,核心代码如下:- /// <summary>
- /// 自定义验证,验证不为空
- /// </summary>
- [AttributeUsage(AttributeTargets.Property)]
- public class RequiredAttribute : AbstractCustomAttribute
- {
- private string _ErrorMessage = "";
- public RequiredAttribute()
- {
- }
- public RequiredAttribute(string ErrorMessage)
- {
- this._ErrorMessage = ErrorMessage;
- }
- /// <summary>
- /// 重写Validate校验方法
- /// </summary>
- /// <param name="value">需要校验的参数</param>
- /// <returns></returns>
- public override FieidEntity Validate(object value)
- {
- if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
- {
- return null;
- }
- return new FieidEntity()
- {
- ErrorMessage = string.IsNullOrWhiteSpace(_ErrorMessage) ? "字段不能为空" : _ErrorMessage,
- };
- }
- }
- /// <summary>
- /// 自定义验证,验证字符长度
- /// </summary>
- [AttributeUsage(AttributeTargets.Property)]
- public class StringLengthAttribute: AbstractCustomAttribute
- {
- private int _MaxLength;
- private int _MinLength;
- private string _ErrorMessage;
- /// <summary>
- ///
- /// </summary>
- /// <param name="MinLength">最小长度</param>
- /// <param name="MaxLength">最大长度</param>
- public StringLengthAttribute(int MinLength,int MaxLength,string ErrorMessage="")
- {
- this._MaxLength = MaxLength;
- this._MinLength = MinLength;
- this._ErrorMessage = ErrorMessage;
- }
- /// <summary>
- /// 重写Validate校验方法
- /// </summary>
- /// <param name="value">需要校验的参数</param>
- /// <returns></returns>
- public override FieidEntity Validate(object value)
- {
- if (value != null && value.ToString().Length >= _MinLength && value.ToString().Length <= _MaxLength)
- {
- return null;
- }
- return new FieidEntity()
- {
- ErrorMessage = string.IsNullOrWhiteSpace(_ErrorMessage) ? $"字段长度必须大于等于{_MinLength}并且小于等于{_MaxLength}" : _ErrorMessage,
- };
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |