Defining Your Own Attribute Class

  1. How we can tell the compiler whether the implementing attribute can be applied?
  2. What are parameters for the AttributeUsageAttribute?

To tell the compiler where the implementing attribute can be applied we can use AttributeUsageAttribute.

 

Example 1:

    namespace System

    {

 

        using System.Reflection;

 

    // Enum used to indicate all the elements of the

    // VOS it is valid to attach this element to.

    [Flags,Serializable]

[System.Runtime.InteropServices.ComVisible(true)]

    public enum AttributeTargets

    {

        Assembly      = 0x0001,

        Module        = 0x0002,

        Class         = 0x0004,

        Struct        = 0x0008,

        Enum          = 0x0010,

        Constructor   = 0x0020,

        Method        = 0x0040,

        Property      = 0x0080,

        Field         = 0x0100,

        Event         = 0x0200,

        Interface     = 0x0400,

        Parameter     = 0x0800,

        Delegate      = 0x1000,

        ReturnValue   = 0x2000,

        GenericParameter = 0x4000,

       

       

        All           = Assembly | Module   | Class | Struct | Enum      | Constructor |

                        Method   | Property | Field | Event  | Interface | Parameter   |

                        Delegate | ReturnValue | GenericParameter,

    }

}

 

 

        /* By default, attributes are inherited and multiple attributes are not allowed */

        [AttributeUsage(AttributeTargets.Class, Inherited = true), Serializable()]

        [System.Runtime.InteropServices.ComVisible(true)]

        public sealed class AttributeUsageAttribute : Attribute

        {

            internal AttributeTargets m_attributeTarget = AttributeTargets.All; // Defaults to all

            internal bool m_allowMultiple = false; // Defaults to false

            internal bool m_inherited = true; // Defaults to true

 

            internal static AttributeUsageAttribute Default = new AttributeUsageAttribute(AttributeTargets.All);

 

            //Constructors

            public AttributeUsageAttribute(AttributeTargets validOn)

            {

                m_attributeTarget = validOn;

            }

            internal AttributeUsageAttribute(AttributeTargets validOn, bool allowMultiple, bool inherited)

            {

                m_attributeTarget = validOn;

                m_allowMultiple = allowMultiple;

                m_inherited = inherited;

            }

 

 

            //Properties

            public AttributeTargets ValidOn

            {

                get { return m_attributeTarget; }

            }

 

            public bool AllowMultiple

            {

                get { return m_allowMultiple; }

                set { m_allowMultiple = value; }

            }

 

            public bool Inherited

            {

                get { return m_inherited; }

                set { m_inherited = value; }

            }

        }

    }

 

 

Example 2:

    [AttributeUsage(AttributeTargets.Method, AllowMultiple=true, Inherited=false)]

    public class MethodAttrAttribute : Attribute {}

 

    internal sealed class SomeType< T>

    {

        //[MethodAttr] We can’t define this attribute for fields

        [field: SomeAttr("Applied to field")]

        public Int32 SomeField = 0;

 

        [MethodAttr]

        [return: SomeAttr("Applied to return value")]

        [method: SomeAttr("Applied to method")]

        public Int32 SomeMethod([param: SomeAttr("Applied to parameter")]

            Int32 SomeParam)

        {

            return SomeParam;

        }

    }

 

The AttributeUsageAttribute has three parameters:

1.      AttributeTargets points out where the attribute can be applied.

2.      AllowMultiple says whether we can define several the same attributes.

3.      Inherited parameter says if the attribute will be applied to members of the derived class, so if the Inhereted parameter is set to false the compiler doesn’t emit attributes code for the derived class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s