Attributes

Hi,

is it possible to somehow retrieve information about the member that the attribute is attached to

Here's example:

[Register("example")]

public static void DoSomething() { ; }

[AttributeUsage( AttributeTargets.Method )]

public RegisterAttribute : Attribute

{

public static Dictionary<string, MethodInfo> Instances = new Dictionary<string, MethodInfo>();

public RegisterAttribute( string name )

{

Instances.Add( name, <what goes here > );

}

}

thanks



Answer this question

Attributes

  • Elie Rodrigue

    Oh, I get it, thanks for your reply.
  • fulano2040

    No it is not possible, even if it was, I don't think this is going to do what you expect. The constructor on the attribute (and hence the Instances.Add statement) will not get called until the first time you retrieve the attribute via the GetCustomAttributes method. The code will not run when the application or type is first initialized, if that is what you are hoping.

  • Attributes