We help IT Professionals succeed at work.

TypeA inherits TypeB :: Cast Dictionary<int, TypeA> to Dictionary<int, TypeB>

Valimai
Valimai asked
on
1,037 Views
Last Modified: 2012-06-21
hello

I have TypeA inherit from TypeB.

My business function returns Dictionary<int, TypeA>
How can i cast it to Dictionary<int, TypeB>?

thanks
Comment
Watch Question

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
you cannot do this directly, just cast the second type when you retrieve it, like:
 
TypeB x = yourDictionary[someIndex] as TypeB;

Author

Commented:
This is not possible. I am using the MVC model and need to send TypeB to the view.
Software Architect
CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
thanks, that helped alot and i will mark that as the solution.

A side-question...

Do you know if I am able to do something like this?
System.Type myType = typeof(TypeA);

foreach (KeyValuePair<int, myType> pair in oldDict)
      newDict[pair.Key] = (TypeB)pair.Value;
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
you cannot this directly. myType is an object of type System.Type that represents a type, but it is not the type itself (hope to be clear), so you cannot use directly into the generic class KeyValuePair. You will need many complex reflection operations to achieve what you want.
 
 

Author

Commented:
thanks!
For reference, I resorted to the following in C# 3.0 based off the above solution.

With the var feature I only have to specify a single type in two places.
var collection = new Dictionary<string,TypeA>();
    foreach(var item in collectionTypeB)
        collection[item.Key] = (TypeA)item.Value;

Open in new window

Author

Commented:
thanks for posting a followup. I have not tried the code yet but see that the first line says "var collection" and the second line says "in collectionTypeB". Is that correct?

I going to checkout my code base to see if i can add this. thanks again.
Valimai, compared to the accepted solution, my code differs as follows:

var collection replaces the variable definition for Dictionary<int, TypeB> newDict
collection replaces newDict
var item  replaces the variable definition for KeyValuePair<int, TypeA> pair
and
in collectionTypeB replaces in oldDict
TypeA in my code refers to the type being converted to. I don't specify the type of the old dictionary, instead relying on var item to figure out type of the old dictionary. In my code it was the case that TypeB inherited from TypeA making this copy conversion safe.
I've adjusted the code snippet to use these variable names.

var newDict = new Dictionary<string,TypeA>();
foreach(var item in oldDict)
    newDict[item.Key] = (TypeA)item.Value;

Open in new window

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.