472,981 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 software developers and data experts.

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?

Sep 25 '07 #1
3 4134
Can you cast it to the IDictionary interface, or IDictionary<TKey,
TValueinterface and then access the Count property (or the
ICollection/ICollection<KeyValuePair<TKey, TValue>interface)?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"wildThought" <an******@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.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?

Sep 25 '07 #2
On Sep 25, 4:50 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Can you cast it to the IDictionary interface, or IDictionary<TKey,
TValueinterface and then access the Count property (or the
ICollection/ICollection<KeyValuePair<TKey, TValue>interface)?

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

"wildThought" <andyb...@gmail.comwrote in message

news:11**********************@d55g2000hsg.googlegr oups.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(Dictionary<,>);
Type[] dictTypes = mi.populatedValue.GetType().GetGenericArguments();

Type theDictType = generic.MakeGenericType(dictTypes);
object o = Activator.CreateInstance(theDictType);
However, I am still stuck with an object. I need a way to get the
count from mi.populatedValue when it is a dictionary as well as call
TryGetValue and access Keys.

Thanks in advance.
Andy
Sep 25 '07 #3
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 GenericDictionaryToNonGenericDictionaryWrapper<TKe y, TValue:
IDictionary
{
// The dictionary.
private Dictionary<TKey, TValuedictionary = null;

public GenericDictionaryToNonGenericDictionaryWrapper(Dic tionary<TKey,
TValuedictionary)
{
this.dictionary = dictionary;
}

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

// 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]
- mv*@spam.guard.caspershouse.com


"wildThought" <an******@gmail.comwrote in message
news:11*********************@y42g2000hsy.googlegro ups.com...
On Sep 25, 4:50 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> Can you cast it to the IDictionary interface, or IDictionary<TKey,
TValueinterface and then access the Count property (or the
ICollection/ICollection<KeyValuePair<TKey, TValue>interface)?

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

"wildThought" <andyb...@gmail.comwrote in message

news:11**********************@d55g2000hsg.googleg roups.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(Dictionary<,>);
Type[] dictTypes = mi.populatedValue.GetType().GetGenericArguments();

Type theDictType = generic.MakeGenericType(dictTypes);
object o = Activator.CreateInstance(theDictType);
However, I am still stuck with an object. I need a way to get the
count from mi.populatedValue when it is a dictionary as well as call
TryGetValue and access Keys.

Thanks in advance.
Andy


Sep 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
4
by: NullQwerty | last post by:
Hi folks, I have a Dictionary which contains a string key and an object value. I want the object value to point to a property in my class and I want it to be by reference, so that later on I...
70
by: jojoba | last post by:
Hello! Does anyone know how to find the name of a python data type. Conside a dictionary: Banana = {} Then, how do i ask python for a string representing the name of the above dictionary...
7
by: noro | last post by:
Is it possible to do the following: for a certain class: ---------------------------- class C: def func1(self): pass def func2(self):
8
by: akameswaran | last post by:
I wrote up a quick little set of tests, I was acutally comparing ways of doing "case" behavior just to get some performance information. Now two of my test cases had almost identical results which...
3
by: Al Meadows | last post by:
I'm using a generic Dictionary object where it is described as: Dictionary<this, thatstuff = new Dictonary<this, that>() Where THIS is a class with several property values. This works fine...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
3
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I have a dictionary (C# 2.0) with an object (an instance of a class) for the key. The class has only a "name" field. dictionary<object, ...> When I use ContainsKey property of the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.