473,795 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<stri ng, 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 6428
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.comwr ote:
I'm trying to define a dictionary whose value is an Generic Action<>
delegate
Then rather than what you posted:
private Dictionary<stri ng, List<Action<T>> >
You would use:

Dictionary<stri ng, 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>o f generic delegates for every key in the
dictionary.

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

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

You would use:

Dictionary<stri ng, 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 MakeGenericMeth od() - but if there
*is* no appropriate T you might have to use "object" or something
comparable.

Marc
Jul 1 '08 #5
eric <7ak@!_df.comwr ote:
I'm trying to postpone the concrete type definition until runtime and I
do want a List<Action<T>o f 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<stri ng,Delegate>

--
Jon Skeet - <sk***@pobox.co m>
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.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
eric <7ak@!_df.comwr ote:
> I'm trying to postpone the concrete type definition until runtime and
I
do want a List<Action<T>o f 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<stri ng,Delegate>

--
Jon Skeet - <sk***@pobox.co m>
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.comwr ote:
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<stri ng,Delegate- that at least gives you
*some* safety...

--
Jon Skeet - <sk***@pobox.co m>
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.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
eric <7ak@!_df.comwr ote:
>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<stri ng,Delegate- that at least gives you
*some* safety...

--
Jon Skeet - <sk***@pobox.co m>
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.comwr ote:
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.co m>
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
15953
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 find the key. In the example below, accessing the dictionary by using the exact key object that was used to add to the dictionary works. (see code comment 1). However, if I attempt to access the dictionary by using a key object that
4
53016
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>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
8
17840
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, MyAbstractClass> { get { return _dictionary; } //error: no implicit conversion. }
1
2541
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 (25,000), and I have 500 such dictionaries. What I've noticed is that the total memory consumed became over 1 GB. When I changed the implementation to List<MyObj>, or SortedList<int,
9
7910
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 List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
7
57556
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 without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
4
4869
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 knows which static class to use for processing. I've tried: static class MyStaticObject {
8
7116
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
6171
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, like List<char>. What I have working is : Type type = newObj.GetType(); //newObj is what I'm trying to dump
6
2988
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 the client side I have tried the following: List<RunningReportreportList = null; localhost.ReportService localrs = new localhost.ReportService();
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.