C# 特性(attribute)

Atrribute表示自定义特性的基类。

其派生类约定惯例以Attribute结尾命名,如:AttributeUsageAttribute、TestAttribute。

下面获取如何定制特性attribute和获取自定义特性信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AttributeDemo
{
    [Test("it's a class1")]
    [Test("it's a class2")]
    [Test("it's a class3")]
    class Program
    {
        [Test("it's a property")]
        public String Name { get; set; }

        static void Main(string[] args)
        {
            Program p = new Program();

            Type type = typeof(Program);
            MethodInfo method = type.GetMethod("AttributeTest");
            MemberInfo member = type.GetMember("Name").First();

            //利于反射分别获取类、方法、属性的算定义特性实例化
            TestAttribute[] myAttr = (TestAttribute[])Attribute.GetCustomAttributes(type, typeof(TestAttribute));
            TestAttribute myAttr2 = (TestAttribute)Attribute.GetCustomAttribute(method, typeof(TestAttribute));
            TestAttribute myAttr3 = (TestAttribute)Attribute.GetCustomAttribute(member, typeof(TestAttribute));

            foreach (TestAttribute attr in myAttr)
            {
                Console.WriteLine(attr.Message);//输出类Program的自定义特性
            }
            Console.WriteLine(myAttr2.Message); //输出方法AttributeTest的自定义特性

            Console.WriteLine(myAttr3.Message); //输出属性Name的自定义特性

            //调用方法AttributeTest
            object obj = Activator.CreateInstance(typeof(Program));
            method.Invoke(obj, null);

            
        }

        [Test("it's a method")]
        public void AttributeTest()
        {
            Console.WriteLine("我还可以这样被调用");
        }
    }

    [AttributeUsageAttribute(AttributeTargets.All,AllowMultiple=true,Inherited=true)]
    public class TestAttribute : Attribute
    {
        public String Message { get; set; }
        public TestAttribute(string message)
        {
            this.Message = message;
        }
    }
}

 

转载于:https://www.cnblogs.com/MrHacker/p/3452813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Attribute特性是一种用于给程序元素添加额外信息的机制。在C#中,Attribute特性通常以Attribute后缀命名,并且可以附加到类、方法、属性等各种程序元素上。当Attribute特性被应用到程序元素上时,它们可以被用来提供元数据,以帮助编译器、运行时或其他代码分析工具进行处理。 引用中提到了一个约定:自定义特性名称应该以Attribute后缀结尾。这是因为在将Attribute特性应用到程序元素上时,编译器首先会查找使用的Attribute的定义,如果找不到,则会尝试查找以"Attribute"结尾的同名特性的定义。 在引用中,提到了Attribute特性在ASP.NET开发中的应用。这表明Attribute特性在ASP.NET开发中是非常常见的,并且被广泛使用。 举个例子,引用给出了一个自定义Attribute特性的示例代码。在这个示例中,定义了一个名为CustomAttribute特性类,它具有一个属性Desc,并提供了两个构造函数。这个示例展示了如何创建自定义Attribute特性,并将其应用到一个名为Student的类上。 总之,Attribute特性C#中用于给程序元素添加元数据,以提供额外的信息和行为。它们是一种强大的工具,可以用于实现各种功能和扩展。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [C# 特性Attribute)](https://blog.csdn.net/qq_42335551/article/details/130268405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Attribute/特性心得随笔](https://download.csdn.net/download/weixin_38607552/13055291)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值