By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,125 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,125 IT Pros & Developers. It's quick & easy.

Gettting dictionary count from an object

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.