By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

Dictionary w/ List of Generic Action<T> delegates as Value

P: n/a
I'm trying to define a dictionary whose value is an Generic Action<>
delegate

private Dictionary<string, List<Action<T>>>

Any ideas on to how specify T or a different collection type more suitable
to this?

Thanks


Jul 1 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
What is the use-case here? What do you want to do with it? For example,
a plain Action might suffice, using the delegate's target for the
instance (perhaps via a closure)?

Marc
Jul 1 '08 #2

P: n/a
On Tue, 01 Jul 2008 09:56:49 -0700, eric <7ak@!_df.comwrote:
I'm trying to define a dictionary whose value is an Generic Action<>
delegate
Then rather than what you posted:
private Dictionary<string, List<Action<T>>>
You would use:

Dictionary<string, Action<T>>

Where "T" is of course an actual type, assuming you want to declare a
concrete version of the generic Dictionary.
Any ideas on to how specify T or a different collection type more
suitable
to this?
Meaning what? You "specify T" by putting a valid concrete type in for the
generic type parameter. If that doesn't answer your question, I think you
need to rephrase the question so that you're more clear about what you're
actually trying to accomplish.

Pete
Jul 1 '08 #3

P: n/a
Sorry,
I'm trying to postpone the concrete type definition until runtime and I
do want a List<Action<T>of generic delegates for every key in the
dictionary.

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 01 Jul 2008 09:56:49 -0700, eric <7ak@!_df.comwrote:
>I'm trying to define a dictionary whose value is an Generic Action<>
delegate

Then rather than what you posted:
>private Dictionary<string, List<Action<T>>>

You would use:

Dictionary<string, Action<T>>

Where "T" is of course an actual type, assuming you want to declare a
concrete version of the generic Dictionary.
>Any ideas on to how specify T or a different collection type more
suitable
to this?

Meaning what? You "specify T" by putting a valid concrete type in for the
generic type parameter. If that doesn't answer your question, I think you
need to rephrase the question so that you're more clear about what you're
actually trying to accomplish.

Pete

Jul 1 '08 #4

P: n/a
Note that Action<T>, as a multicast delegate, can already support
multiple delegates per instance. Re the generics - what is the
difficulty? If the T is only known at runtime, then you'll need to use
reflection and MakeGenericType() or MakeGenericMethod() - but if there
*is* no appropriate T you might have to use "object" or something
comparable.

Marc
Jul 1 '08 #5

P: n/a
eric <7ak@!_df.comwrote:
I'm trying to postpone the concrete type definition until runtime and I
do want a List<Action<T>of generic delegates for every key in the
dictionary.
As Marc said, you can probably just use a multi-cast delegate to avoid
needing a List.

However, the point about generics is to give you *compile-time* safety.
If you won't know the type at compile time, other than that it's a
delegate, you should probably just make it a
Dictionary<string,Delegate>

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 1 '08 #6

P: n/a
I swapped out the Dictionary for Hashtable and that was able to give me what
I was after.

Thanks to all for the help.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
eric <7ak@!_df.comwrote:
> I'm trying to postpone the concrete type definition until runtime and
I
do want a List<Action<T>of generic delegates for every key in the
dictionary.

As Marc said, you can probably just use a multi-cast delegate to avoid
needing a List.

However, the point about generics is to give you *compile-time* safety.
If you won't know the type at compile time, other than that it's a
delegate, you should probably just make it a
Dictionary<string,Delegate>

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Jul 1 '08 #7

P: n/a
eric <7ak@!_df.comwrote:
I swapped out the Dictionary for Hashtable and that was able to give me what
I was after.
Well that's just going completely nongeneric.

Why not use Dictionary<string,Delegate- that at least gives you
*some* safety...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 1 '08 #8

P: n/a
I couldn't make Delegate work with Action<T>. I was really trying to
implement something where
I didn't need to know about a specific delegate type. The end result is
really not as nongeneric in the untype-safe sense as you might think.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
eric <7ak@!_df.comwrote:
>I swapped out the Dictionary for Hashtable and that was able to give me
what
I was after.

Well that's just going completely nongeneric.

Why not use Dictionary<string,Delegate- that at least gives you
*some* safety...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Jul 1 '08 #9

P: n/a
eric <7ak@!_df.comwrote:
I couldn't make Delegate work with Action<T>. I was really trying to
implement something where
I didn't need to know about a specific delegate type.
Which is why you'd need Delegate instead of Action<T>. Then just invoke
it dynamically, which I suspect is what you're having to do anyway.
The end result is really not as nongeneric in the untype-safe sense
as you might think.
You're using Hashtable, which is completely nongeneric and untypesafe.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 1 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.