C# attribute用法

贴代码

using System;
using System.Reflection;

namespace my_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("http;//my.com/about/method")]
        public void MyMethod()
        {
            return;
        }
    }
    class Program
    {
        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

你可能感兴趣的:(C# attribute用法)