我在創建動態對象時遇到了問題。請找到下面的代碼,創建對象的動態屬性
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);
}
那裏有第二個foreach循環,我需要爲所有活動生成屬性。例如,如果member.ActivityList中有4個活動,則需要爲對象生成4個屬性。
myObject.Activity1 = 10;
myObject.Activity2 = 20;
myObject.Activity3 = 30;
myObject.Activity4 = 40;
我該怎麼做?我在這裏做錯了什麼?
Regards, Karthik。