特性(Attribute)

特性(Attribute)

常用的系统特性

Serializable:常用于标记类,表示可被序列化

Nonserialized :不允许序列化,在被标注为Serializable序列化的类中,某字段前加Nonserialized,表示该字段不允许序列化。

Obsolete: 标记方法,被标记的方法在调用时会产生警告或报错。

    [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
    [Serializable]
    public class MyAttribute
    {
        [NonSerialized] //仅对字段有效
        public int MyProperty;

        [Obsolete("这个方法不要用了,我设置了报错",true)]
        public void Show()
        {
            Console.WriteLine("test");
        }

    }

AttributeUsage:也是一个特性,是用来修饰特性的特性,一般标记在特性类上
AttributeTargets指定当前特性只能标记在某个地方(属性、方法、字段等地方);AttributeTargets.All表示可标记在任意地方
AllowMultiple:是否可以重复标记
Inherited:是否可以继承特性

自定义特性

1.每使用一个特性标记一次都相当于实例化了一个特性对象

2.可以通过反射获取被标记的特性。

namespace MyAttribute
{
    [User(100)]
    [User("我是类上的特性")]
    public class UserInfor
    {
        [User(200)]
        [User("我是属性上的特性")]
        public int MyProperty { get; set; }


        [User(300)]
        [User("我是方法上的特性")]
        public void Show()
        {

        }
    }
    
    [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
    public class UserAttribute: Attribute
    {
        public int Id = 0;
        public string Name { get; set; } = "我是空的";
        public string PassWord { get; set; }
        public UserAttribute()
        {
            
        }
         public UserAttribute( int Id)
        {
            this.Id = Id;
        }
         public UserAttribute(string Name)
        {
            this.Name = Name;
        }
        public string Show()
        {
            return this.PassWord;
        }
    }
}
namespace MyAttribute
{
    internal class Program
    {
        static void Main(string[] args)
        {
            UserInfor userInfor = new UserInfor();

            Type type = userInfor.GetType();
           
            if (type.IsDefined(typeof(UserAttribute),true)) //判断type这个类上是否有UserAttribute的标记
            {
                //GetCustomAttributes:获取标记在类外层的特性   true表示继承的也算
                foreach (UserAttribute item in type.GetCustomAttributes(true))
                {
                    Console.WriteLine($"{item.Id}");
                    Console.WriteLine($"{item.Name}");
                }
            }

            foreach (var item in type.GetProperties())
            {
                //判断item这个属性上是否有UserAttribute的标记
                if (item.IsDefined(typeof(UserAttribute), true))
                {
                    //获取标记在属性上的所有特性   true表示继承的也算
                    foreach (UserAttribute userAttribute in item.GetCustomAttributes(true))
                    {
                        Console.WriteLine($"{userAttribute.Id}");
                        Console.WriteLine($"{userAttribute.Name}");
                    }
                }
            }
            foreach (var item in type.GetMethods())
            {
                //判断item这个方法上是否有UserAttribute的标记
                if (item.IsDefined(typeof(UserAttribute), true))
                {
                    //获取标记在属性上的所有特性   true表示继承的也算
                    foreach (UserAttribute userAttribute in item.GetCustomAttributes(true))
                    {
                        Console.WriteLine($"{userAttribute.Id}");
                        Console.WriteLine($"{userAttribute.Name}");
                    }
                }
            }
            Console.ReadLine();
        }
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值