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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eric

    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




  • Marc Gravell

    #2
    Re: Dictionary w/ List of Generic Action&lt;T&gt; delegates as Value

    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

    Comment

    • Peter Duniho

      #3
      Re: Dictionary w/ List of Generic Action&lt;T&gt; delegates as Value

      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

      Comment

      • eric

        #4
        Re: Dictionary w/ List of Generic Action&lt;T&gt; delegates as Value

        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" <NpOeStPeAdM@nn owslpianmk.comw rote in message
        news:op.udmh5xw 78jd0ej@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

        Comment

        • Marc Gravell

          #5
          Re: Dictionary w/ List of Generic Action&lt;T&gt; delegates as Value

          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

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Dictionary w/ List of Generic Action&lt;T&gt; delegates as Value

            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 - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon_skeet
            C# in Depth: http://csharpindepth.com

            Comment

            • eric

              #7
              Re: Dictionary w/ List of Generic Action&lt;T&gt; delegates as Value

              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]" <skeet@pobox.co mwrote in message
              news:MPG.22d47c f66c8c2fb8dd8@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 - <skeet@pobox.co m>
              Web site: http://www.pobox.com/~skeet
              Blog: http://www.msmvps.com/jon_skeet
              C# in Depth: http://csharpindepth.com

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Dictionary w/ List of Generic Action&lt;T&gt; delegates as Value

                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 - <skeet@pobox.co m>
                Web site: http://www.pobox.com/~skeet
                Blog: http://www.msmvps.com/jon_skeet
                C# in Depth: http://csharpindepth.com

                Comment

                • eric

                  #9
                  Re: Dictionary w/ List of Generic Action&lt;T&gt; delegates as Value

                  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]" <skeet@pobox.co mwrote in message
                  news:MPG.22d496 966c1590d6ddc@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 - <skeet@pobox.co m>
                  Web site: http://www.pobox.com/~skeet
                  Blog: http://www.msmvps.com/jon_skeet
                  C# in Depth: http://csharpindepth.com

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Dictionary w/ List of Generic Action&lt;T&gt; delegates as Value

                    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 - <skeet@pobox.co m>
                    Web site: http://www.pobox.com/~skeet
                    Blog: http://www.msmvps.com/jon_skeet
                    C# in Depth: http://csharpindepth.com

                    Comment

                    Working...