For Programmers: Free Programming Magazines  


Home > Archive > C# > September 2005 > How to obtain the method an attribute is assigned to









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to obtain the method an attribute is assigned to
Jason Bell

2005-09-11, 6:57 pm

Give the following custom attribute:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class WidgetEventAttribute : System.Attribute
{
protected Cegui.EventHandler mEventHandler = null;

public WidgetEventAttribute( string widgetname, string
eventname )
{
mEventHandler = new Cegui.EventHandler();
mEventHandler.Subscribe(
WindowManager.Instance.getWindow(widgetname), eventname );
}

public WidgetEventAttribute( Cegui.Window window, string
eventname )
{
mEventHandler = new Cegui.EventHandler();
mEventHandler.Subscribe( window, eventname );
}
}

That's used in the following way:


[WidgetEvent(this, Cegui.PushButton.EventClicked)]
protected bool HandleClicked()
{
Console.WriteLine("Oh ye of little faith!");
return true;
}

How would I know what method the particular attribute is assigned to?
WidgetEventAttribute is intended to create a delegate to the function
it's assigned to, and marshal that delegate as a function pointer to an
unmanaged library for a callback.

I've seen lots of examples where one reflects to find all methods, then
gets a list of attributes assigned to it. I'd like to do the opposite:
from within the a particular instance of WidgetEventAttribute, find what
method it's assigned to.

Before anyone asks, I have my reasons for doing it this way, largely due
to the weird way this library handles events.
Jan Bannister

2005-09-12, 7:59 am

Hi Jason,

I've looked into reflecting attributes and noticed the same issue. I
wasn't able to see away of doing this. I think that the attributes
themselves don't have the information within their structure to point
back to the entities they are decorating.

I was investigating walking back up the stack trace to investigate the
call site when i had to go off and do some real work :P. That might be
a good place to start.

Tell me if you find away of doing it.

Good Luck
Jan

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com