473,408 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Finding the method an attribute is assigned to

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.
Sep 11 '05 #1
8 1814
Jason,
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.


There's no built in way to retrieve that information. If the attribute
instance must know that, the code that retrieves the attribute
instance has to call a method or set a property to provide the
matching MethodInfo object.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 11 '05 #2
Jason Bell <ce******@shaw.ca> wrote:

<snip>
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.


I don't believe there's any way of doing that. I don't think there's
even any guarantee that you get one instance of the attribute per time
it's referenced. For instance, it would make sense for the CLI to just
create one instance of the ThreadStaticAttribute and use that
everywhere it's applied.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 11 '05 #3
Thanks for the input.

What I don't get, is how does the DllImport attribute work then? Surely it
must have to act on the function it's assigned to in some manner.

Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
Jason Bell <ce******@shaw.ca> wrote:

<snip>
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.


I don't believe there's any way of doing that. I don't think there's
even any guarantee that you get one instance of the attribute per time
it's referenced. For instance, it would make sense for the CLI to just
create one instance of the ThreadStaticAttribute and use that
everywhere it's applied.


Sep 11 '05 #4
Jason Bell <ce******@shaw.ca> wrote:
Thanks for the input.

What I don't get, is how does the DllImport attribute work then? Surely it
must have to act on the function it's assigned to in some manner.


That's an attribute the CLR has special knowledge of, so it can do
extra things with it at load time.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 11 '05 #5
"Jon Skeet [C# MVP]" wrote:
I don't think there's
even any guarantee that you get one instance of the attribute per time
it's referenced.
No guarantee, perhaps, but that's the effect. If an attribute has
writable properties and you change the properties on an attribute
instance, you'll get the compiled-in values next time you retrieve
that attribute with GetCustomAttributes.
For instance, it would make sense for the CLI to just
create one instance of the ThreadStaticAttribute and use that
everywhere it's applied.


I know what you mean, but this is a bad example, imho - it would make
sense for the CLI to NEVER create a ThreadStaticAttribute instance,
but just look for it in the metadata when loading a type.

--

www.midnightbeach.com
Sep 11 '05 #6
Jason Bell wrote:
What I don't get, is how does the DllImport attribute work then? Surely it
must have to act on the function it's assigned to in some manner.


It's easy to go from method to attribute ....

--

www.midnightbeach.com
Sep 11 '05 #7
Jon Shemitz <jo*@midnightbeach.com> wrote:
"Jon Skeet [C# MVP]" wrote:
I don't think there's
even any guarantee that you get one instance of the attribute per time
it's referenced.


No guarantee, perhaps, but that's the effect. If an attribute has
writable properties and you change the properties on an attribute
instance, you'll get the compiled-in values next time you retrieve
that attribute with GetCustomAttributes.


Well, that's the current behaviour - but as I say, I don't believe
that's guaranteed. Now, if it's not guaranteed, it would be valid to
create a CLI implementation which didn't do that. If there were some
method specified to go from attribute to what it was applied to, there
would be no valid way to implement it in such a CLI implementation.
For instance, it would make sense for the CLI to just
create one instance of the ThreadStaticAttribute and use that
everywhere it's applied.


I know what you mean, but this is a bad example, imho - it would make
sense for the CLI to NEVER create a ThreadStaticAttribute instance,
but just look for it in the metadata when loading a type.


Except, of course, that you have to be able to retrieve *an* instance
of ThreadStaticAttribute if you ask for the custom attributes for the
field.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 11 '05 #8
Hi Jason,

You posted this on another group and i replied that you shold try the
StackTrace. I was interested enough to give it a go and Stack trace
does work. Not sure if it will be any more efficient but you can give
it a go. From within you Attribute ctor try this:

System.Diagnostics.StackTrace t = new
System.Diagnostics.StackTrace();
Console.WriteLine(t.ToString());

Just my 2 pence,
Jan

Sep 12 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim Williams | last post by:
How can I find a function's name from within the function? >>> print container.trailing_period.__name__ trailing_period >>> print container.trailing_period.func_name trailing_period but...
7
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
8
by: Jason Bell | last post by:
Give the following custom attribute: public class WidgetEventAttribute : System.Attribute { protected Cegui.EventHandler mEventHandler = null; public WidgetEventAttribute( string...
2
by: lcaamano | last post by:
We have a tracing decorator that automatically logs enter/exits to/from functions and methods and it also figures out by itself the function call arguments values and the class or module the...
1
by: Stedak | last post by:
We have a WinForm application that we would like to add security to. We would also like to set up a configuration utility that would make it easier to assign or remove rights. When using code...
3
by: John Machin | last post by:
I have stumbled across some class definitions which include all/most method names in a __slots__ "declaration". A cut-down and disguised example appears at the end of this posting. Never mind...
3
by: AWasilenko | last post by:
I'm still in the process of learning python via a handful of books I bought. One book I am reading just introduced Base Class Methods. I found that I needed more understanding on this concept and...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.