c# - Multi-dimension IReadOnlyDictionary typed using an interface for the inner value type -


this little tricky put words, i'll explain example. following code not compile:

var data = new[] {         new {item = "abc", values = new[] {1,2,3}},         new {item = "def", values = new[] {1,2,3}}};  ireadonlydictionary<string, ireadonlydictionary<guid, int>> target;  target = new readonlydictionary<string, ireadonlydictionary<guid, int>>(     data.todictionary(             => i.item,             v => new readonlydictionary<guid, int>(                 v.values.todictionary(                     => guid.newguid(),                     b => b)))); 

the error is:

the best overloaded method match     'readonlydictionary<string,ireadonlydictionary<guid,int>>         .readonlydictionary(idictionary<string,ireadonlydictionary<system.guid,int>>)' has invalid arguments 

yet if declare target using class instead of interface inner values, compiles:

ireadonlydictionary<string, readonlydictionary<guid, int>> target;  target = new readonlydictionary<string, readonlydictionary<guid, int>>(     data.todictionary(             => i.item,             v => new readonlydictionary<guid, int>(                 v.values.todictionary(                     => guid.newguid(),                     b => b)))); 

why can't use interface inner dictionary?

you can cast readonlydictionary ireadonlydictionary:

target = new readonlydictionary<string, ireadonlydictionary<guid, int>>(     data.todictionary(             => i.item,             v => (ireadonlydictionary<guid, int>)new readonlydictionary<guid, int>(                 v.values.todictionary(                     => guid.newguid(),                     b => b)))); 

or specify interface type generic argument todictionary:

target = new readonlydictionary<string, ireadonlydictionary<guid, int>>(     data.todictionary<string, ireadonlydictionary<guid, int>>(             => i.item,             v => new readonlydictionary<guid, int>(                 v.values.todictionary(                     => guid.newguid(),                     b => b)))); 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -