Search code examples
c#castingidictionarymemorycache

How to cast IDictionary<string, object> to IDictionary<string, MyType>


I'm using the default MemoryCache, which works fine for my purposes. However the GetValues() method returns IDictionary<string, object>

Is there a quick way to cast this to IDictionary<string, MyType> ?


Solution

  • Use ToDictionary:

    ToDictionary(k => k.Key, k => (MyType)k.Value)