Detecting the Use of a Custom Attribute Without Creating Attribute-Derived Objects

 

1. How to detect attribute parameters without creating its instance?

There are some methods which allow detection attributes and their values without creating an instance of attribute. To achieve this goal we can call the CustomAttributeData.GetCustomAttributes() method which returns         IList<CustomAttributeData>. Enumerating this list we can get the constructor parameters and properties values of an attribute.

Example 1:

    class Program

    {

        static void Main(string[] args)

        {

            ShowAttributes(typeof(SomeType<object>));

 

            MemberInfo[] mis = typeof(SomeType<object>).GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

 

            foreach (var member in mis)

            {

                ShowAttributes(member);

            }

 

            Console.ReadKey();

        }

 

        public static void ShowAttributes(MemberInfo attributeTarget)

        {

            IList<CustomAttributeData> customAttributeData = CustomAttributeData.GetCustomAttributes(attributeTarget);

 

            foreach (var attributeDataItem in customAttributeData)

            {

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

                Trace.WriteLine(attributeDataItem);

 

                foreach (var constructorArgument in attributeDataItem.ConstructorArguments)

                {

                    Trace.WriteLine("Constructor argument: " + constructorArgument);

                }

 

                foreach (var namedArgument in attributeDataItem.NamedArguments)

                {

                    Trace.WriteLine("NamedArgument: " + namedArgument);

                }

            }

           

        }

 

Output:

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

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

Constructor argument: "Applied to type"

Constructor argument: "29.06.2010"

NamedArgument: Optional = "Named Parameter"

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

[AttDetectionWithoutCreation.MethodAttrAttribute()]

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

[AttDetectionWithoutCreation.SomeAttr("Applied to method")]

Constructor argument: "Applied to method"

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

[AttDetectionWithoutCreation.SomeAttr("Applied to get accessor method")]

Constructor argument: "Applied to get accessor method"

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

[AttDetectionWithoutCreation.SomeAttr("Applied to compiler-generated add & remove methods")]

Constructor argument: "Applied to compiler-generated add & remove methods"

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

[AttDetectionWithoutCreation.SomeAttr("Applied to compiler-generated add & remove methods")]

Constructor argument: "Applied to compiler-generated add & remove methods"

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

[System.Runtime.TargetedPatchingOptOutAttribute("Performance critical to inline across NGen image boundaries")]

Constructor argument: "Performance critical to inline across NGen image boundaries"

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

[System.Runtime.TargetedPatchingOptOutAttribute("Performance critical to inline across NGen image boundaries")]

Constructor argument: "Performance critical to inline across NGen image boundaries"

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

[System.Security.SecuritySafeCriticalAttribute()]

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

[System.Runtime.ConstrainedExecution.ReliabilityContractAttribute((System.Runtime.ConstrainedExecution.Consistency)3, (System.Runtime.ConstrainedExecution.Cer)2)]

Constructor argument: (System.Runtime.ConstrainedExecution.Consistency)3

Constructor argument: (System.Runtime.ConstrainedExecution.Cer)2

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

[System.Security.SecuritySafeCriticalAttribute()]

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

[AttDetectionWithoutCreation.SomeAttr("Applied to property")]

Constructor argument: "Applied to property"

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

[AttDetectionWithoutCreation.SomeAttr("Applied to event")]

Constructor argument: "Applied to event"

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

[AttDetectionWithoutCreation.SomeAttr("Applied to field")]

Constructor argument: "Applied to field"

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

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

Constructor argument: "Applied to compiler-generated field"

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