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

  • Thread starter Thread starter eric
  • Start date Start date
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
 
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
 
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.
 
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
 
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>
 
I swapped out the Dictionary for Hashtable and that was able to give me what
I was after.

Thanks to all for the help.
 
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...
 
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.
 
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.
 
Back
Top