473,383 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 4155
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.