c# - Creating Dynamic properties for object -
i have problem in creating dynamic objects. please find below code,
list<object> memberslist = new list<object>(); foreach(var members in activitymembers){ dynamic myobject = new system.dynamic.expandoobject(); myobject.membernumber = members.membernumber; myobject.membername = members.name; foreach (var activity in members.activitylist) { myobject.[activity.activityname] = activity.minutes; } memberslist.add(myobject); }
there inside second foreach loop, need generate properties activities. example if there 4 activities in members.activitylist, 4 properties needs generated object.
myobject.activity1 = 10; myobject.activity2 = 20; myobject.activity3 = 30; myobject.activity4 = 40;
how can this? did wrong here?
regards, karthik.
remove .
when indexing object i.e. change
myobject.[activity.activityname] = activityminutes;
to
myobject[activity.activityname] = activity.minutes;
actually won't solve problem straight away, compile fine when attempt run throw runtimebinderexception
can't index expandoobject
directly. need cast dictionary before iterating (that's is) e.g.
var dict = (idictionary<string, object>)myobject; ... dict[activity.activityname] = activity.minutes;
Comments
Post a Comment