I have a dictionary object like :-
var gridContent = new Dictionary<T, KeyValuePair<int, bool>>();
Now I want to typeCast this into :-
Dictionary<SomeClass, KeyValuePair<int, bool>>();
Is there any way to do so ?
I have a dictionary object like :-
var gridContent = new Dictionary<T, KeyValuePair<int, bool>>();
Now I want to typeCast this into :-
Dictionary<SomeClass, KeyValuePair<int, bool>>();
Is there any way to do so ?
Im sure there is a way to cast this in a different way, but the first thing that came to my mind is to convert it using Linq:
PS> scriptcs
scriptcs (ctrl-c to exit or :help for help)
> public class SomeClass {}
> public class SomeClassB : SomeClass {}
> public class Test<T> where T: SomeClass
* {
* public Dictionary<T, KeyValuePair<int, bool>> gridContent = new Dictionary<T, KeyValuePair<int, bool>>();
* }
>
> var testInstance = new Test<SomeClassB>();
> // casting error
> // var testGridContent = (Dictionary<SomeClass, KeyValuePair<int, bool>>)testInstance.gridContent;
>
> var testGridContent = testInstance.gridContent.ToDictionary(kvp=>(SomeClass)kvp.Key, kvp=>kvp.Value);