PC Review


Reply
Thread Tools Rate Thread

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

 
 
eric
Guest
Posts: n/a
 
      1st Jul 2008
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




 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      1st Jul 2008
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
 
Reply With Quote
 
eric
Guest
Posts: n/a
 
      1st Jul 2008
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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Tue, 01 Jul 2008 09:56:49 -0700, eric <7ak@!_df.com> wrote:
>
>> 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



 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      1st Jul 2008
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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st Jul 2008
eric <7ak@!_df.com> wrote:
> 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 - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
eric
Guest
Posts: n/a
 
      1st Jul 2008
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> eric <7ak@!_df.com> wrote:
>> 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 - <(E-Mail Removed)>
> Web site: http://www.pobox.com/~skeet
> Blog: http://www.msmvps.com/jon_skeet
> C# in Depth: http://csharpindepth.com



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st Jul 2008
eric <7ak@!_df.com> wrote:
> 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 - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
eric
Guest
Posts: n/a
 
      1st Jul 2008
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> eric <7ak@!_df.com> wrote:
>> 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 - <(E-Mail Removed)>
> Web site: http://www.pobox.com/~skeet
> Blog: http://www.msmvps.com/jon_skeet
> C# in Depth: http://csharpindepth.com



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st Jul 2008
eric <7ak@!_df.com> wrote:
> 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 - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Q re Thread Safe Dictionary and delegates Simon Woods Microsoft C# .NET 4 18th Jan 2010 12:19 PM
Generic delegates and Remoting =?Utf-8?B?Sm9obiBXLg==?= Microsoft C# .NET 0 27th Sep 2007 06:20 PM
What's the difference between these two ways of adding delegates to a Generic List object? Deckarep Microsoft C# .NET 1 21st Jan 2007 04:08 AM
Problem using Reflection to Assign Generic Delegates Joanna Carter \(TeamB\) Microsoft C# .NET 6 17th Feb 2005 09:57 PM
Generic Event Delegates and Events msnews.microsoft.com Microsoft C# .NET 0 30th Dec 2004 02:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:09 AM.