C# Attribute

一 使用Attribute的例子

1 用在类上的

[Serializable]
public sealed class String:IComparable,Iconeable,IConvertible,
Ienumerable

2 用再方法上的

[STAThread]
static void Main()

二 Attribute

Attribute 是与类、结构、方法等元素相关的额外信息,是对元信息的扩展。
通过Attribute可以使程序、甚至语言本身的功能得到增强。

[HelpUr]("http://SomeUr/APIDocs/SomeClass")
class SomeClass
{
    [WebMethod]
    void GetCustomers(){...}
    string Test([SomeAttr]string param1){...}
}

三 使用系统定义的Attribute

1 使用Attribute的一般方式

① 在程序集、类、域、方法等前面用[]表示;
② 可以省略"Attribute"几个字母,只写xxxxx;
③ 可以带参数
位置参数(相当于构造方法带的参数);
命名参数(域名或属性名=值);

2 示例

① 在Main()方法使用[STAThread];
② 使用“过时”:AttributeObsolete.cs;
③ 使用“条件”:AttributeConditional.cs;
④ 在结构上、枚举上使用:StructLayout,Flag;
⑤ 在程序集级别应用Attribute;
[assembly:AssemblyCompany(“”)];

四 自定义Attribute

1 声明Attribute类

① 从System.Attribute继承而来;
② 名字要用xxxxAttribute;

2 使用Attribute类

① 在类即成员上面使用方括号;
② 可以省略后缀Attribute;

3 通过反射访问属性

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

namespace Attribute定义及使用
{
    [AttributeUsage(AttributeTargets.Class
        | AttributeTargets.Method,
    AllowMultiple = true)]

    public class HelpAttribute:System.Attribute
    {
        public readonly string Url;
        private string topic;
        public string Topic
        {
            get { return topic; }
            set { topic = value; }
        }

        public HelpAttribute(string url)
        {
            this.Url = url;
        }
    }

    [HelpAttribute("https://msvc/MyClassInfo",Topic ="Test"),
        Help("https://my.com/about/class")]

    class MyClass
    {
        [Help("https://my.com/about/method")]
        public void MyMethod(int i)
        {
            return;
        }
    }

    public class MemberInfo_GetCustomAttributes
    {
        public static void Main()
        {
            Type myType = typeof(MyClass);

            object[] attributes = myType.GetCustomAttributes(false);
            for(int i=0;i<attributes.Length;i++)
            {
                PrintAttributeInfo(attributes[i]);
            }

            MemberInfo[] myMembers = myType.GetMembers();

            for (int i=0;i<myMembers.Length;i++)
            {
                Console.WriteLine("\nNumber{0}:", myMembers[i]);
                Object[] myAttributes = myMembers[i].GetCustomAttributes(false);
                for(int j=0;j<myAttributes.Length;j++)
                {
                    PrintAttributeInfo(myAttributes[j]);
                }
            }
            Console.ReadKey();
        }

        static void PrintAttributeInfo(object attr)
        {
            if(attr is HelpAttribute)
            {
                HelpAttribute attrh = (HelpAttribute)attr;

                Console.WriteLine("----Url:" + attrh.Url + "Topic:" + attrh.Topic);
            }
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值