Using reflection to retrieve constructor used to instantiate attribute

Posted by summatix on Stack Overflow See other posts from Stack Overflow or by summatix
Published on 2010-05-15T01:48:17Z Indexed on 2010/05/15 1:54 UTC
Read the original article Hit count: 605

Filed under:
|
|

How can I retrieve information about how an attribute was instantiated?

Consider I have the following class definitions:

[AttributeUsage(AttributeTargets.Class)]
public class ExampleAttribute : Attribute
{
    public ExampleAttribute(string value) { Value = value; }
    public string Value { get; private set; }
}

[ExampleAttribute("test")]
public class Test { }

The new .NET 4.0 MemberInfo.GetCustomAttributesData method:

foreach (var attribute in typeof(Test).GetCustomAttributesData())
{
    Console.WriteLine(attribute);
}

outputs [Example.ExampleAttribute("test")]. Is there another way to retrieve this same information, preferably using the MemberInfo.GetCustomAttributes method?

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection