自定义特性Attribute

特性attribute就是一个类,可以应用在程序集,类,属性,方法等元素。

在运行的时候通过反射获取特性attribute的信息。

下面示例是对属性做Trim标记,可以在对象层面调用一次Trim(),就将标记了Trim特性的属性作Trim()处理。

using System;
namespace ConsoleApp1
{
    //定制特性,特性也是一个类,自定义特性须继承System.Attribute
    //AttributeUsage控制特性
    [AttributeUsage(AttributeTargets.Property,//应用于属性,
        AllowMultiple = true,//允许应用多次
        Inherited = false)]//不能继承到派生类
    public class TrimAttribute : Attribute
    {
        private readonly Type type;
        //必须显示定义构造函数
        public TrimAttribute(Type type)
        {
            this.type = type;
        }
        public Type Type => this.type;

    }
    public static class TrimExtension
    {
        //拓展方法
        public static void TrimProperties(this object obj)
        {
            //反射确定特性的信息
            Type t = obj.GetType();
            foreach (var pro in t.GetProperties())
            {
                foreach (var attr in pro.GetCustomAttributes(true))//返回用于此属性的自定义特性
                {
                    if (attr is TrimAttribute ta && ta.Type == typeof(string))//如果此属性附加了Trim(typeof(string))的属性
                    {
                        if (pro.GetValue(obj) != null)
                        {
                            pro.SetValue(obj, pro.GetValue(obj).ToString().Trim(), null);//trim属性值
                        }
                    }
                }
            }
        }
    }
    public class Test
    {
        [Trim(typeof(string))]//[]里其实就是TrimAttribute的构造函数
        public string testString1 { get; set; }
        public string testString2 { get; set; }
        [Trim(typeof(string))]
        public string testString3 { get; set; }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test() { testString1 = "  testString1-   ", testString2 = "testString2-   ", testString3 = "     testString3  " };
            Console.WriteLine(test.testString1 + test.testString2 + test.testString3);
            test.TrimProperties();
            Console.WriteLine(test.testString1 + test.testString2 + test.testString3);
            //看结果可以发现,Test类被标记了Trim特性的属性都被Trim()。
            Console.ReadKey();
        }
    }
}

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值