Gettting dictionary count from an object

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

    #1

    Gettting dictionary count from an object

    If I have an object that contains a generic dictionary inside of it,
    how do I get access to its properties such as count?

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Gettting dictionary count from an object

    Can you cast it to the IDictionary interface, or IDictionary<TKe y,
    TValueinterface and then access the Count property (or the
    ICollection/ICollection<Key ValuePair<TKey, TValue>interfac e)?


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "wildThough t" <andyblum@gmail .comwrote in message
    news:1190752846 .906691.176780@ d55g2000hsg.goo glegroups.com.. .
    If I have an object that contains a generic dictionary inside of it,
    how do I get access to its properties such as count?
    >

    Comment

    • wildThought

      #3
      Re: Gettting dictionary count from an object

      On Sep 25, 4:50 pm, "Nicholas Paldino [.NET/C# MVP]"
      <m...@spam.guar d.caspershouse. comwrote:
      Can you cast it to the IDictionary interface, or IDictionary<TKe y,
      TValueinterface and then access the Count property (or the
      ICollection/ICollection<Key ValuePair<TKey, TValue>interfac e)?
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - m...@spam.guard .caspershouse.c om
      >
      "wildThough t" <andyb...@gmail .comwrote in message
      >
      news:1190752846 .906691.176780@ d55g2000hsg.goo glegroups.com.. .
      >
      >
      >
      If I have an object that contains a generic dictionary inside of it,
      how do I get access to its properties such as count?- Hide quoted text -
      >
      - Show quoted text -
      Nick,

      Thanks. I am a little confused though. If I have a generic
      dictionary in the object. I can even reconstruct the dictioary
      objects type using reflection. Since I am using reflection. I will
      not know the type before hand.

      For example, I can do this:

      Type generic = typeof(Dictiona ry<,>);
      Type[] dictTypes = mi.populatedVal ue.GetType().Ge tGenericArgumen ts();

      Type theDictType = generic.MakeGen ericType(dictTy pes);
      object o = Activator.Creat eInstance(theDi ctType);


      However, I am still stuck with an object. I need a way to get the
      count from mi.populatedVal ue when it is a dictionary as well as call
      TryGetValue and access Keys.

      Thanks in advance.


      Andy


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Gettting dictionary count from an object

        wildThought,

        It seems like you don't know at run time what the types are that are
        used for the type parameters. Because of that, you will have to use
        reflection to access these members.

        I do remember one time proposing to Mads (from MS) that you should be
        able to call members on generic types which do not require type parameters,
        almost using an unqualified generic type as an interface. So in this case,
        you could use the type Dictionary<TKey , TValueas an interface, and call
        the Count property, but not the Add method (as that requires information
        about the fully qualified type).

        One way to get around this would be to create a wrapper, like so:

        public class GenericDictiona ryToNonGenericD ictionaryWrappe r<TKey, TValue:
        IDictionary
        {
        // The dictionary.
        private Dictionary<TKey , TValuedictionar y = null;

        public GenericDictiona ryToNonGenericD ictionaryWrappe r(Dictionary<TK ey,
        TValuedictionar y)
        {
        this.dictionary = dictionary;
        }

        public int Count
        {
        get
        {
        // Return the count.
        return dictionary.Coun t;
        }
        }

        // And so on..
        }

        The point here is to create a wrapper for your non-generic dictionary
        which you can use to make early-bound calls. You would use reflection to
        create an instance of this class and instantiate it with your dictionary,
        and then cast it to the IDictionary interface and use that directly.

        Or, if you don't have a need for the generic instance anywhere, you can
        just use a Hashtable, and not worry about any of this.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m




        "wildThough t" <andyblum@gmail .comwrote in message
        news:1190754451 .615412.77910@y 42g2000hsy.goog legroups.com...
        On Sep 25, 4:50 pm, "Nicholas Paldino [.NET/C# MVP]"
        <m...@spam.guar d.caspershouse. comwrote:
        > Can you cast it to the IDictionary interface, or IDictionary<TKe y,
        >TValueinterfac e and then access the Count property (or the
        >ICollection/ICollection<Key ValuePair<TKey, TValue>interfac e)?
        >>
        >--
        > - Nicholas Paldino [.NET/C# MVP]
        > - m...@spam.guard .caspershouse.c om
        >>
        >"wildThought " <andyb...@gmail .comwrote in message
        >>
        >news:119075284 6.906691.176780 @d55g2000hsg.go oglegroups.com. ..
        >>
        >>
        >>
        If I have an object that contains a generic dictionary inside of it,
        how do I get access to its properties such as count?- Hide quoted
        text -
        >>
        >- Show quoted text -
        >
        Nick,
        >
        Thanks. I am a little confused though. If I have a generic
        dictionary in the object. I can even reconstruct the dictioary
        objects type using reflection. Since I am using reflection. I will
        not know the type before hand.
        >
        For example, I can do this:
        >
        Type generic = typeof(Dictiona ry<,>);
        Type[] dictTypes = mi.populatedVal ue.GetType().Ge tGenericArgumen ts();
        >
        Type theDictType = generic.MakeGen ericType(dictTy pes);
        object o = Activator.Creat eInstance(theDi ctType);
        >
        >
        However, I am still stuck with an object. I need a way to get the
        count from mi.populatedVal ue when it is a dictionary as well as call
        TryGetValue and access Keys.
        >
        Thanks in advance.
        >
        >
        Andy
        >
        >

        Comment

        Working...