473,395 Members | 1,418 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,395 software developers and data experts.

delegate in custom attribute

I have a custom attribute which i use for fields in an enum.

I want to pass around a delegate in these things.

so i want to do something like this

[MyCustomAttrb(string name, string desc, int value, delegate clickEvent)]

Is this possible? i'm having no luck.
Apr 19 '07 #1
10 9919
Nathan Laff <re******@hotmail.comwrote:
I have a custom attribute which i use for fields in an enum.

I want to pass around a delegate in these things.

so i want to do something like this

[MyCustomAttrb(string name, string desc, int value, delegate clickEvent)]

Is this possible? i'm having no luck.
No. Attribute parameters are limited by the spec:

<quote>
The types of positional and named parameters for an attribute class are
limited to the attribute parameter types, which are:

* One of the following types: bool, byte, char, double, float, int,
long, short, string.
* The type object.
* The type System.Type.
* An enum type, provided it has public accessibility and the types
in which it is nested (if any) also have public accessibility.
* Single-dimensional arrays of the above types.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 19 '07 #2
It doesn't mean that you can't expose a method/event which will take the
delegate though. You can get the attribute through reflection and then pass
your delegate to the method/event.

However, it's kind of pointless, since you can't make a correlation
between the attribute and what it's assigned to (from within the attribute,
that is).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP***********************@msnews.microsoft.co m...
Nathan Laff <re******@hotmail.comwrote:
>I have a custom attribute which i use for fields in an enum.

I want to pass around a delegate in these things.

so i want to do something like this

[MyCustomAttrb(string name, string desc, int value, delegate clickEvent)]

Is this possible? i'm having no luck.

No. Attribute parameters are limited by the spec:

<quote>
The types of positional and named parameters for an attribute class are
limited to the attribute parameter types, which are:

* One of the following types: bool, byte, char, double, float, int,
long, short, string.
* The type object.
* The type System.Type.
* An enum type, provided it has public accessibility and the types
in which it is nested (if any) also have public accessibility.
* Single-dimensional arrays of the above types.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 19 '07 #3
No. Attribute parameters are limited by the spec:
>
<quote>
The types of positional and named parameters for an attribute class are
limited to the attribute parameter types, which are:

* One of the following types: bool, byte, char, double, float, int,
long, short, string.
* The type object.
* The type System.Type.
* An enum type, provided it has public accessibility and the types
in which it is nested (if any) also have public accessibility.
* Single-dimensional arrays of the above types.
</quote>
That can't be entirely true as it builds with a Delegate type in the
attribute contructor? I just can't seem to get anything to work right in it
:P

Apr 19 '07 #4
Nathan Laff <re******@hotmail.comwrote:
No. Attribute parameters are limited by the spec:

<quote>
The types of positional and named parameters for an attribute class are
limited to the attribute parameter types, which are:

* One of the following types: bool, byte, char, double, float, int,
long, short, string.
* The type object.
* The type System.Type.
* An enum type, provided it has public accessibility and the types
in which it is nested (if any) also have public accessibility.
* Single-dimensional arrays of the above types.
</quote>

That can't be entirely true as it builds with a Delegate type in the
attribute contructor? I just can't seem to get anything to work right in it
:P
What do you mean by "builds with a delegate type in the attribute
constructor"? Do you mean typeof(SomeDelegate)? If so, that's of type
System.Type, covered by the above.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 19 '07 #5
What do you mean by "builds with a delegate type in the attribute
constructor"? Do you mean typeof(SomeDelegate)? If so, that's of type
System.Type, covered by the above.
No, I do it like this...

[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
public class ActionItemAttribute : Attribute
{
public ActionItemAttribute(string name, string description,
string buttonText, Delegate buttonClickEvent, int value)
{
this.name = name;
this.description = description;
this.buttonText = buttonText;
this.buttonClickEvent = buttonClickEvent;
this.value = value;
}

Apr 19 '07 #6
Nathan Laff <re******@hotmail.comwrote:
What do you mean by "builds with a delegate type in the attribute
constructor"? Do you mean typeof(SomeDelegate)? If so, that's of type
System.Type, covered by the above.

No, I do it like this...
<snip>

Okay - but you can't specify those parameters using [ActionItem(...)].
That's the bit of the spec I was referring to.

Arguably the compiler shouldn't allow your code to compile, given that:
<quote>
Each public instance constructor for an attribute class defines a valid
sequence of positional parameters for that attribute class.
</quote>

Your public instance constructor clearly *isn't* a valid sequence of
positional parameters, given that Delegate isn't a valid type for a
positional parameter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 19 '07 #7
<snip>
>
Okay - but you can't specify those parameters using [ActionItem(...)].
That's the bit of the spec I was referring to.

Arguably the compiler shouldn't allow your code to compile, given that:
<quote>
Each public instance constructor for an attribute class defines a valid
sequence of positional parameters for that attribute class.
</quote>

Your public instance constructor clearly *isn't* a valid sequence of
positional parameters, given that Delegate isn't a valid type for a
positional parameter.
Ok, that makes sense. Thanks.

Any other idea of how I can accomplish the same type of thing? I want an
event associated with an enum item. No way to do it? :(

Apr 19 '07 #8
Nathan Laff <re******@hotmail.comwrote:
Your public instance constructor clearly *isn't* a valid sequence of
positional parameters, given that Delegate isn't a valid type for a
positional parameter.

Ok, that makes sense. Thanks.

Any other idea of how I can accomplish the same type of thing? I want an
event associated with an enum item. No way to do it? :(
A simple map from enum to delegate?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 19 '07 #9
A simple map from enum to delegate?

Yeah yeah! how would I do that? I'm new to C# from Delphi so I'm not all
that familiar with what .NET has to offer yet.

Apr 19 '07 #10
Nathan Laff <re******@hotmail.comwrote:
A simple map from enum to delegate?

Yeah yeah! how would I do that? I'm new to C# from Delphi so I'm not all
that familiar with what .NET has to offer yet.
Well, Dictionary<YourEnumType,Delegatewould seem like a good start,
assuming you're using .NET 2.0. Otherwise, just a Hashtable.

However, I'd slow down at this point and read a book which will cover
collection types etc. You'll find you'll save a lot of time in the long
run by taking a step back early on, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 19 '07 #11

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

Similar topics

3
by: F. Da Costa | last post by:
Hi, I was wondering *why* there is a difference between the results of the following two statements. On the suface they seem to do the same (or do they?) frm => returns void ...
10
by: John Bowman | last post by:
Hello, I need some help getting a callback delegate passed as an argument to a dynamically linked Dll method so it in turn, can eventually call it. Below is the salient portions of code I'm...
1
by: Pieter Breed | last post by:
Hi All, I am trying to use the attribute/reflection system to as much potential as I can think of, but I've run into a snag. I would appreciate it if someone would point me in the right...
3
by: Edward Diener | last post by:
I understand the syntax of custom attributes, but I have no idea what they are supposed to do. Anyone care to give me a clue as to their functionality ?
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
7
by: sam.m.gardiner | last post by:
I'm working with VB.NET events and I want a way to disconnect all the handlers of an event. I want to do this in the object that is the source of the event. This is slightly tricky in VB.Net as the...
5
by: =?Utf-8?B?cGFnYXRlcw==?= | last post by:
Hello All, I am sure that I am just overlooking something, but here's something I can't quite get right... I want to be able to get the value of a parameter of an unknown custom attribute at...
3
by: Curious | last post by:
Hi, I have a program that gets the prices of stocks in real-time. The program consists of the following key components: // Constructor public GetRealTimeStockPrice() { // Step 1
7
by: colin | last post by:
Hi, How can I use a delegate that I can set to call a non static function but of any instance of the class ? eg class TypeTeader<T> { delegate T readDelegate<T>();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.