Detecting the Use of a Custom Attribute

  1. What is efficient method to detect a custom attribute?
  2. What are the methods to detect attributes and its parameters?

To detect whether attribute is applied we can use the IsDefined method. This method is efficient because it doesn’t construct any instance of the attribute class.

Example 1:

Trace.WriteLine(typeof(SomeType<Object>).IsDefined(typeof(SomeAttr), false));

 

Output:

            true

 

There are also some useful methods which allow checking parameters, fields and properties of the applied attribute.

 

Method

Description

IsDefined

Determines whether any custom attributes of a specified type are applied to an assembly, module, type member, or method parameter.

GetCustomAttribute

Retrieves a custom attribute of a specified type applied to an assembly, module, type member, or method parameter.

GetCustomAttributes

Retrieves an array of the custom attributes applied to an assembly, module, type member, or method parameter.

 

When we call these methods they scan metadata performing string comparisons. Obviously it’s inefficient, so is better to cache the result of calling these methods. They have inherit parameter when it is set to true the methods search the ancestors of element for custom attributes.

Example 2:

 

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

    public class MethodAttrAttribute : Attribute {}

 

    [type: SomeAttr("Applied to type", "29.06.2010", Optional="Named Parameter")]

    internal sealed class SomeType<[typevar: SomeAttr("Applied to generic type variable")] T>

    {

        //[MethodAttr]

        [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;

        }

 

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

        public String SomeProp

        {

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

            get { return null; }

        }

 

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

        [field: SomeAttr("Applied to compiler-generated field")]

        [method: SomeAttr("Applied to compiler-generated add & remove methods")]

        public event EventHandler SomeEvent;

    }

 

            MemberInfo[] mi = typeof(SomeType<object>).GetMembers();

 

            foreach (MemberInfo memberInfo in mi)

            {

                var attributes = Attribute.GetCustomAttributes(memberInfo);

                foreach (Attribute attribute in attributes)

                {

                    Trace.WriteLine("Member: " + memberInfo + "; Attribute type: " + attribute.GetType().ToString() + "; Attribute data ( " + attribute + ")");

                    Trace.WriteLine("-====================================================================================-");

                }

            }

 

Output:

 

Member: Int32 SomeMethod(Int32); Attribute type: Attributes.SomeAttr; Attribute data ( Applied to: Applied to method; User Creation date:; Attribute creation date: 29.06.2010 16:29:58; Optional: )

-====================================================================================-

Member: Int32 SomeMethod(Int32); Attribute type: Attributes.MethodAttrAttribute; Attribute data ( Attributes.MethodAttrAttribute)

-====================================================================================-

Member: System.String get_SomeProp(); Attribute type: Attributes.SomeAttr; Attribute data ( Applied to: Applied to get accessor method; User Creation date:; Attribute creation date: 29.06.2010 16:29:58; Optional: )

-====================================================================================-

Member: Void add_SomeEvent(System.EventHandler); Attribute type: Attributes.SomeAttr; Attribute data ( Applied to: Applied to compiler-generated add & remove methods; User Creation date:; Attribute creation date: 29.06.2010 16:29:58; Optional: )

-====================================================================================-

Member: Void remove_SomeEvent(System.EventHandler); Attribute type: Attributes.SomeAttr; Attribute data ( Applied to: Applied to compiler-generated add & remove methods; User Creation date:; Attribute creation date: 29.06.2010 16:29:58; Optional: )

-====================================================================================-

Member: Boolean Equals(System.Object); Attribute type: System.Runtime.TargetedPatchingOptOutAttribute; Attribute data ( System.Runtime.TargetedPatchingOptOutAttribute)

-====================================================================================-

Member: Int32 GetHashCode(); Attribute type: System.Runtime.TargetedPatchingOptOutAttribute; Attribute data ( System.Runtime.TargetedPatchingOptOutAttribute)

-====================================================================================-

Member: System.Type GetType(); Attribute type: System.Security.SecuritySafeCriticalAttribute; Attribute data ( System.Security.SecuritySafeCriticalAttribute)

-====================================================================================-

Member: System.String SomeProp; Attribute type: Attributes.SomeAttr; Attribute data ( Applied to: Applied to property; User Creation date:; Attribute creation date: 29.06.2010 16:29:58; Optional: )

-====================================================================================-

Member: System.EventHandler SomeEvent; Attribute type: Attributes.SomeAttr; Attribute data ( Applied to: Applied to event; User Creation date:; Attribute creation date: 29.06.2010 16:29:58; Optional: )

-====================================================================================-

Member: Int32 SomeField; Attribute type: Attributes.SomeAttr; Attribute data ( Applied to: Applied to field; User Creation date:; Attribute creation date: 29.06.2010 16:29:58; Optional: )

-====================================================================================-

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