特性(Attribute)

1.枚举获取特性,转换值

using System;
using System.Reflection;

namespace MyAttribute
{
    public static class HelloExtesion
    {
        public static string GetStr(this Enum e)
        {
            Type type = typeof(UserState);
            FieldInfo p = type.GetField(e.ToString());
            if (p.IsDefined(typeof(CSAttribute), true))
            {
                CSAttribute cs = p.GetCustomAttribute(typeof(CSAttribute)) as CSAttribute;
                return cs.Name;
            }
            else
            {
                return e.ToString();
            }
        }
    }
    public enum UserState
    {
        [CS("富人")]
        Rich = 1,
        [CS("一般人")]
        Normal = 2,
        //[CS("穷人")]
        Poor = 3,
    }
    public class CSAttribute : Attribute
    {
        public string Name { set; get; }
        public CSAttribute(string Name)
        {
            this.Name = Name;
        }
    }
}
View Code

 

2.特性验证字段格式

using HomeWork1.AttributeModels;
using System;
using System.Reflection;

namespace HomeWork1.Tool
{
    public static class ValidateTool
    {
        public static void ValidateEntity<T>(this T t) where T : class
        {
            Type type = typeof(T);
            foreach (var prop in type.GetProperties())
            {
                if(prop.IsDefined(typeof(AbstractAttribute),true))
                {
                    foreach (AbstractAttribute item in prop.GetCustomAttributes(typeof(AbstractAttribute)))
                    {
                        string s = item.Validate(prop.GetValue(t));
                        if (!string.IsNullOrEmpty(s))
                        {
                            throw new Exception($"{prop.GetChinese()}({prop.Name}):{s}");
                        }
                    }
                }
            }
        }
        public static string GetColumnName(this PropertyInfo Prop)
        {
            if (Prop.IsDefined(typeof(ColumnAttribute), true))
            {
                return (Prop.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute).Name;
            }
            return Prop.Name;
        }
        public static string GetTableName(this Type type)
        {
            if (type.IsDefined(typeof(TableNameAttribute), true))
            {
                return (type.GetCustomAttribute(typeof(TableNameAttribute)) as TableNameAttribute).Name;
            }
            return type.Name;
        }
        public static string GetChinese(this PropertyInfo Prop)
        {
            if (Prop.IsDefined(typeof(ChineseAttribute), true))
            {
                return (Prop.GetCustomAttribute(typeof(ChineseAttribute)) as ChineseAttribute).Name;
            }
            return Prop.Name;
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/Jacob-Wu/p/9301843.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值