c# - ExpandoObject quirky behavior while adding items dynamically -
so i'm using expandoobject add properties dynamically populated string. going use these bind datagrid later. given below 2 variations of code, doing same thing (to me atleast) 1 fails , 1 doesn't. can me understand why is?
failed code
dynamic dynamo = new expandoobject() idictionary<string,object>; string words[] = basestring.split('|'); foreach(string word in words) { dynamo[word] = word.toupperinvariant(); }
successful code
dynamic dynamo = new expandoobject(); var dynamoose = dynamo as idictionary<string,object>; //notice cast string words[] = basestring.split('|'); foreach(string word in words) { dynamoose[word] = word.toupperinvariant(); }
`
your code doesn't run directly. couldn't double cast work. , string words[] didn't compile, changed string[] words.
from there on failing code gave me valuable info:
additional information: cannot apply indexing [] expression of type 'system.dynamic.expandoobject'
your second piece of code works, because have instance of idictionary while failing piece tries call []-indexing operator on dynamic.
Comments
Post a Comment