C# 自定义特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {

        [FieldName("UsersName")]
        public string UsersName
        { get; set; }


        [FieldName2("Test2", Comment = "This is a test code")]  /*Comment 指定可选参数,编译器不会使用Comment来构造生成特性,而是在特性中寻找一个public 的属性进行赋值*/
        public int Age
        {
            get;set;
        }
        static void Main(string[] args)
        {
        }
    }



    [AttributeUsage(                               /* 这个是只用于修饰Attribute的特性     */
    AttributeTargets.Property,                 /* All Assembly Class Constuctor Delegate Enum Event Field GenericParameter Interface Method Parameter Property ReturnValue Struct
                                                      不同程序元素 可以使用 |           assembly 和 module使用时 要在前面显示标记 */
    AllowMultiple = true,                       /* 可以将同一个特性多次标记一个目标      [FieldName("asd")][FieldName("dsdsdsc")] public string Name{get;set;}   */
    Inherited = true                            /* 设置为 true ,继承 类或接口可以获得该特性*/

    )]
    internal class FieldNameAttribute : Attribute
    {
        string _name;
        public FieldNameAttribute(string name)
        {
            this._name = name;
        }
    }





    internal class FieldName2Attribute : Attribute
    {

        public string Comment
        { get; set; }

        public string _name;
        public FieldName2Attribute(string name)
        {
            this._name = name;
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值