示例代码
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace MyAttribute.Extension { /// <summary> /// 用户状态 /// </summary> public enum UserState { /// <summary> /// 正常 /// </summary> [Remark("正常")] Normal = 0,//左边是字段名称 右边是数据库值 哪里放描述? 注释 /// <summary> /// 冻结 /// </summary> [Remark("冻结")] Frozen = 1, /// <summary> /// 删除 /// </summary> //[Remark("删除")] Deleted = 2 } //枚举项加一个描述 实体类的属性也可以Display //别名 映射 public class RemarkAttribute : Attribute { public RemarkAttribute(string remark) { this._Remark = remark; } private string _Remark = null; public string GetRemark() { return this._Remark; } } public static class RemarkExtension { public static string GetRemark(this Enum value) { Type type = value.GetType(); FieldInfo field = type.GetField(value.ToString()); if (field.IsDefined(typeof(RemarkAttribute), true)) { RemarkAttribute attribute = (RemarkAttribute)field.GetCustomAttribute(typeof(RemarkAttribute), true); return attribute.GetRemark(); } else { return value.ToString(); } } } }
using MyAttribute.Extension; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyAttribute { /// <summary> /// 这里是注释,除了让人看懂这里写的是什么, /// 对运行没有任何影响 /// </summary> //[Obsolete("请不要使用这个了,请使用什么来代替", true)]//影响编译器的运行 //[Serializable]//可以序列化和反序列化 可以影响程序的运行 //MVC filter ORM table key display wcf //[Custom] //[Custom()] //[Custom(123), Custom(123, Description = "1234")] [Custom(123, Description = "1234", Remark = "2345")]//方法不行 public class Student { [CustomAttribute] public int Id { get; set; } [Leng(5,10)]//还有各种检查 public string Name { get; set; } [Leng(20, 50)] public string Accont { get; set; } /// <summary> /// 10001~999999999999 /// </summary> [Long(10001, 999999999999)] public long QQ { get; set; } //private long _QQ2 = 0;//解决数据合法性,给属性增加了太多的事儿 //public long QQ2 //{ // get // { // return this._QQ2; // } // set // { // if (value > 10001 && value < 999999999999) // { // _QQ2 = value; // } // else // { // throw new Exception(); // } // } //} [CustomAttribute] public void Study() { Console.WriteLine($"这里是{this.Name}"); } [Custom()] [return: Custom()] public string Answer([Custom]string name) { return $"This is {name}"; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace MyAttribute.Extension { public static class ValidateExtension { public static bool Validate(this object oObject) { Type type = oObject.GetType(); foreach (var prop in type.GetProperties()) { if (prop.IsDefined(typeof(AbstractValidateAttribute), true)) { object[] attributeArray = prop.GetCustomAttributes(typeof(AbstractValidateAttribute), true); foreach (AbstractValidateAttribute attribute in attributeArray) { if (!attribute.Validate(prop.GetValue(oObject))) { return false;//表示终止 } } } //if (prop.IsDefined(typeof(LongAttribute), true)) //{ // LongAttribute attribute = (LongAttribute)prop.GetCustomAttribute(typeof(LongAttribute), true); // if (!attribute.Validate(prop.GetValue(oObject))) // { // return false; // } //} //if (prop.IsDefined(typeof(LengAttribute), true)) //{ // LengAttribute attribute = (LengAttribute)prop.GetCustomAttribute(typeof(LengAttribute), true); // if (!attribute.Validate(prop.GetValue(oObject))) // { // return false; // } //} } return true; } } public abstract class AbstractValidateAttribute : Attribute { public abstract bool Validate(object value); } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class LengAttribute : AbstractValidateAttribute { private int _Min = 0; private int _Max = 0; public LengAttribute(int min, int max) { this._Min = min; this._Max = max; } public override bool Validate(object value)//" " { if (value != null && !string.IsNullOrWhiteSpace(value.ToString())) { int length = value.ToString().Length; if (length > this._Min && length < this._Max) { return true; } } return false; } } public class LongAttribute : AbstractValidateAttribute { private long _Min = 0; private long _Max = 0; public LongAttribute(long min, long max) { this._Min = min; this._Max = max; } public override bool Validate(object value)//" " { //可以改成一句话 if (value != null && !string.IsNullOrWhiteSpace(value.ToString())) { if (long.TryParse(value.ToString(), out long lResult)) { if (lResult > this._Min && lResult < this._Max) { return true; } } } return false; } } }