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

E

eric

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
 
M

Marc Gravell

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
 
E

eric

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.
 
M

Marc Gravell

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
 
J

Jon Skeet [C# MVP]

eric said:
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>
 
E

eric

I swapped out the Dictionary for Hashtable and that was able to give me what
I was after.

Thanks to all for the help.
 
J

Jon Skeet [C# MVP]

eric said:
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...
 
E

eric

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.
 
J

Jon Skeet [C# MVP]

eric said:
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.

You're using Hashtable, which is completely nongeneric and untypesafe.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top