如何将 ExpandoObject 添加到现有的 ExpandoObject?
How can I add a ExpandoObject to an already existing ExpandoObject?
这是我的代码:
dynamic App = new ExpandoObject();
//App names are SAP, CRM and ERP
//App names - adding static
//App.SAP = new ExpandoObject();
//App.CRM = new ExpandoObject();
//App.ERP = new ExpandoObject();
在最后 4 行中,我添加了一个 ExpandoObject
静态,因为我之前知道应用名称。但我想动态地执行此操作。
我可以为属性做动态的:
AddProperty(App.SAP, "Name", "sap name");
AddProperty(App.SAP, "UserID", "sap userid");
AddProperty(App.SAP, "EmailID", "userid@sap.com");
AddProperty(App.SAP, "GroupOf", "group1, group2, group3");
public static void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
{
// ExpandoObject supports IDictionary so we can extend it like this
var expandoDict = expando as IDictionary<string, object>;
if (expandoDict.ContainsKey(propertyName))
expandoDict[propertyName] = propertyValue;
else
expandoDict.Add(propertyName, propertyValue);
}
但我需要将此对象 App.SAP
添加到此 App,它也是一个 ExpandoObject
,因此我可以稍后动态添加 App.CRM
或 App.ERP
。
你会得到
ExpandoObject
doesn't have a definition for SAP
...
采用这种方法:
AddProperty(App.SAP, "Name", "sap name");
因为您没有声明 APP
ExpandoObject 中的SAP
。
相反,您可以尝试通过提供 parentName
来修改 AddProperty
方法并处理嵌套对象。
AddProperty(App, "SAP", "Name", "sap name");
AddProperty(App, "SAP", "UserID", "sap userid");
AddProperty(App, "SAP", "EmailID", "userid@sap.com");
AddProperty(App, "SAP", "GroupOf", "group1, group2, group3");
public static void AddProperty(ExpandoObject expando, string parentName, string propertyName, object propertyValue)
{
// ExpandoObject supports IDictionary so we can extend it like this
var expandoDict = expando as IDictionary<string, object>;
if (expandoDict.ContainsKey(parentName))
{
((IDictionary<string, object>)expandoDict[parentName])[propertyName] = propertyValue;
}
else
{
Dictionary<string, object> child = new Dictionary<string, object>
{
{ propertyName, propertyValue }
};
expandoDict.Add(parentName, child);
}
}
这是我的代码:
dynamic App = new ExpandoObject();
//App names are SAP, CRM and ERP
//App names - adding static
//App.SAP = new ExpandoObject();
//App.CRM = new ExpandoObject();
//App.ERP = new ExpandoObject();
在最后 4 行中,我添加了一个 ExpandoObject
静态,因为我之前知道应用名称。但我想动态地执行此操作。
我可以为属性做动态的:
AddProperty(App.SAP, "Name", "sap name");
AddProperty(App.SAP, "UserID", "sap userid");
AddProperty(App.SAP, "EmailID", "userid@sap.com");
AddProperty(App.SAP, "GroupOf", "group1, group2, group3");
public static void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
{
// ExpandoObject supports IDictionary so we can extend it like this
var expandoDict = expando as IDictionary<string, object>;
if (expandoDict.ContainsKey(propertyName))
expandoDict[propertyName] = propertyValue;
else
expandoDict.Add(propertyName, propertyValue);
}
但我需要将此对象 App.SAP
添加到此 App,它也是一个 ExpandoObject
,因此我可以稍后动态添加 App.CRM
或 App.ERP
。
你会得到
ExpandoObject
doesn't have a definition forSAP
...
采用这种方法:
AddProperty(App.SAP, "Name", "sap name");
因为您没有声明 APP
ExpandoObject 中的SAP
。
相反,您可以尝试通过提供 parentName
来修改 AddProperty
方法并处理嵌套对象。
AddProperty(App, "SAP", "Name", "sap name");
AddProperty(App, "SAP", "UserID", "sap userid");
AddProperty(App, "SAP", "EmailID", "userid@sap.com");
AddProperty(App, "SAP", "GroupOf", "group1, group2, group3");
public static void AddProperty(ExpandoObject expando, string parentName, string propertyName, object propertyValue)
{
// ExpandoObject supports IDictionary so we can extend it like this
var expandoDict = expando as IDictionary<string, object>;
if (expandoDict.ContainsKey(parentName))
{
((IDictionary<string, object>)expandoDict[parentName])[propertyName] = propertyValue;
}
else
{
Dictionary<string, object> child = new Dictionary<string, object>
{
{ propertyName, propertyValue }
};
expandoDict.Add(parentName, child);
}
}