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

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

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
9 6403
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
8
by: Brian P | last post by:
I want to expose a property of Dictionary<string, MyAbstractClass>. I tried to do it this way: private Dictionary<string, MyChildClass> _dictionary; public Dictionary<string,...
1
by: Eran | last post by:
Hi, I have a huge data structure, which I previosly stored in a Dictionary<int, MyObj> MyObj is relatively small (2 int, 1 DateTime, 1 bool). The dictionary I am using is quite large...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
4
by: Mark S. | last post by:
Hello, I have a series of changing string IDs that are loaded dynamically a couple times a minute. I need to associate each ID with a different static class so later on in the app's lifecycle it...
8
by: Peter Larsen [CPH] | last post by:
Hi, How do i concat two dictionaries aka the following sample: Dictionary<int, stringa; Dictionary<int, stringb; b.Add(a); What is the easiest way to concat two dictionaries ??
2
by: Fred Mellender | last post by:
I am trying to use reflection to output the fields (names and values) of an arbitrary object -- an object dump to a TreeView. It works pretty well, but I am having trouble with generic lists,...
6
by: Peter | last post by:
I have a WebService which returns a List of RunningReport class How do I read this XML data on the client side. How do I convert List<RunningReportfrom the WebService side to List<RunningReporton...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.