这是我的代码:
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添加到这个应用程序中,这个应用程序也是一个ExpandoObject,所以以后我可以动态地添加App.CRM或App.ERP。
发布于 2022-03-31 07:50:45
你会得到
ExpandoObject没有SAP的定义.
采用这一办法:
AddProperty(App.SAP, "Name", "sap name");因为您没有在SAP中声明APP ExpandoObject。
相反,您可以尝试通过提供AddProperty并处理嵌套对象来修改parentName方法。
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);
}
}https://stackoverflow.com/questions/71688317
复制相似问题