用到了一些反射:(自己看吧)
public enum UserState { /// <summary> /// 正常 /// </summary> [Remark("正常")] Normal = 0,//左边是字段名称 右边是数据库值 哪里放描述? 注释 /// <summary> /// 冻结 /// </summary> [Remark("冻结")] Frozen = 1, /// <summary> /// 删除 /// </summary> //[Remark("删除")] Deleted = 2 } 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(); } } } public abstract class AbstractValidateAttribute : Attribute { public abstract bool Validate(object oValue); } public class LongValidateAttribute : AbstractValidateAttribute { private long _lMin = 0; private long _lMax = 0; public LongValidateAttribute(long lMin, long LMax) { this._lMin = lMin; this._lMax = LMax; } public override bool Validate(object oValue) { return this._lMin < (long)oValue && (long)oValue < this._lMax; } } public class RequirdValidateAttribute : AbstractValidateAttribute { public override bool Validate(object oValue) { return oValue != null; } } public class DataValidate { public static bool Validate<T>(T t) { Type type = t.GetType(); bool result = true; foreach (var prop in type.GetProperties()) { if (prop.IsDefined(typeof(AbstractValidateAttribute), true)) { object item = prop.GetCustomAttributes(typeof(AbstractValidateAttribute), true)[0]; AbstractValidateAttribute attribute = item as AbstractValidateAttribute; if (!attribute.Validate(prop.GetValue(t))) { result = false; break; } } } return result; } }