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

Can't use delegate in attribute

Hello.

I want an enum where each member corresponds to a method call - which
seems reasonable enough.

I tried something along these lines:

internal class MyAttribute : Attribute
{
public delegate void MyDelegate(int i);
MyDelegate m_d = null;
public MyAttribute(MyDelegate d) { m_d = d; }
}

// ... in another class ...
public enum Something
{
[MyAttribute(new MyAttribute.MyDelegate(SomeMethod))] Blah
}
private static void SomeMethod(int i) { }

However, I'm not allowed to create that MyDelegate within the enum,
because "an attribute argument must be a constant expression, typeof
expression or array creation expression".

What would you folks suggest as a workaround?

P.
Nov 17 '05 #1
4 2023
Perhaps you could elaborate on what you are trying to achieve here.

Nov 17 '05 #2
Perhaps you could elaborate on what you are trying to achieve here.

Nov 17 '05 #3
"Mohammad" <m@abdulfatah.net> wrote:
Perhaps you could elaborate on what you are trying to
achieve here.


As I said, I want an enum where each member corresponds to a method
call.

P.
Nov 17 '05 #4
Thank you for pointing out that to me again, but that is not what I
meant. Obviously you are trying to solve a bigger problem here using
this technique, and, as you have noticed, it can't be done. My question
was about the bigger picture, in hope of finding another way to solve
your problem.

Anyway, if you must, an alternative might be to use reflection. If the
methods you want to call through the delegates are static, then you
could use an attribute that specifies the assembly name, fully
qualified class name, and method name as strings. And then use
reflection to invoke it at runtime.

But that's just jumping through too many hoops for my taste.

Nov 17 '05 #5

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

Similar topics

4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
1
by: Daniel | last post by:
How do I call a delegate in late bound C# dll? there some way to do this w/ a sharedinterface file? any examples? i tried this but it doesnt work: (oType.GetMethod("IOCTLJOB").Invoke(pObj, new...
3
by: N8 | last post by:
I am trying to get an exception to occur and consequently found that when adding a target method to a delegates invocation list, a copy of that object is added instead of a reference to the object....
0
by: Paul E Collins | last post by:
Hello. I want an enum where each member corresponds to a method call - which seems reasonable enough. I tried something along these lines: internal class MyAttribute : Attribute { public...
2
by: Russell Hind | last post by:
I have a delegate which I use to store a current 'state' function (for a statemachine inside a form). __delegate void State_t(const Message_c& Message); I assign to it such as m_State = new...
3
by: Jeff S | last post by:
Please consider this sample code: It registers a delegate with an event. p1.FirstNameChanged += new Person.NameChanged(p1_FirstNameChanged); Now the following code removes the delegate:...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
10
by: Nathan Laff | last post by:
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 Is this possible? i'm having no luck.
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...
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
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
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.