delegate in custom attribute

N

Nathan Laff

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

Jon Skeet [C# MVP]

Nathan Laff said:
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>
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)


Jon Skeet said:
Nathan Laff said:
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>
 
N

Nathan Laff

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
 
J

Jon Skeet [C# MVP]

Nathan Laff said:
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.
 
N

Nathan Laff

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;
}
 
J

Jon Skeet [C# MVP]

Nathan Laff said:
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.
 
N

Nathan Laff

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? :(
 
J

Jon Skeet [C# MVP]

Nathan Laff said:
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?
 
N

Nathan Laff

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

Jon Skeet [C# MVP]

Nathan Laff said:
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,Delegate> would 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top