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
Post a Comment