Custom Attributes : Attribute « Attribute « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Text;

    [AttributeUsage(AttributeTargets.Class |
                    AttributeTargets.Constructor |
                    AttributeTargets.Field |
                    AttributeTargets.Method |
                    AttributeTargets.Property,
                    AllowMultiple = true)]
    public class BugFixAttribute : System.Attribute
    {
        public int      BugID;
        public string   Date;
        public string   Programmer;
        public string   Comment;

        public BugFixAttribute(int bugID,string programmer,string date){
            this.BugID = bugID;
            this.Programmer = programmer;
            this.Date = date;
        }
    }
    [BugFixAttribute(1, "B", "01/04/05",Comment = "value")]
    public class MyMath
    {
        public double DoFunc1(double param1)
        {
            return param1 + DoFunc2(param1);
        }

        public double DoFunc2(double param1)
        {
            return param1 / 3;
        }
    }








10.1.Attribute
10.1.1.Use Attributes to mark a method
10.1.2.Assembly-Level Attributes
10.1.3.Custom Attributes
10.1.4.Providing an Attribute Constructor
10.1.5.Retrieving a Specific Attribute and Checking Its Initialization
10.1.6.Saving a Document Using System.SerializableAttribute