【问题标题】:Construct a dynamic object from another object that has dictionary<string, object> property with varying key-value pairs从另一个对象构造一个动态对象,该对象具有具有不同键值对的 dictionary<string, object> 属性
【发布时间】:2019-06-04 19:14:56
【问题描述】:

我正在尝试从包含一些静态属性的现有对象中整理属性。该对象还包含一个带有键值对的字典。这些键值对应该作为属性动态添加到我正在创建的新对象中。

这是源对象的设置:

public class MyClass
{
    public string PropA { get; set; }
    public string PropB { get; set; }
    public string PropC { get; set; }

    public IDictionary<string, NameValuePair> PropD { get; set; }
}

public class NameValuePair
{
    public string Name { get; set; }
    public string Value { get; set; }
}

这是我创建对象列表的地方:

void Main()
{

    var data = new List<MyClass>
    {
        new MyClass {PropA = "A1", PropB = "B1", PropC = "C1", PropD = new Dictionary<string, NameValuePair>
        {
            { "Code11", new NameValuePair {Name = "PropD11", Value = "V11"}},
            { "Code12", new NameValuePair {Name = "PropD12", Value = "V12"}}
        }},
        new MyClass {PropA = "A2", PropB = "B2", PropC = "C2", PropD = new Dictionary<string, NameValuePair>
        {
            { "Code21", new NameValuePair {Name = "PropD21", Value = "V21"}},
            { "Code22", new NameValuePair {Name = "PropD22", Value = "V22"}},
        }},
        new MyClass {PropA = "A3", PropB = "B3", PropC = "C3", PropD = new Dictionary<string, NameValuePair>
        {
            { "Code12", new NameValuePair {Name = "PropD12", Value = "V31"}},
            { "Code21", new NameValuePair {Name = "PropD21", Value = "V32"}},
        }},
    };
//Extract column names from static properties
    var staticColumns = typeof(MyClass).GetProperties().Where(n => n.PropertyType == typeof(string)).Select(p => p.Name);

//Extract column names from the dictionary
    var dynamicColumns = data.Where(c => c.PropD.Any()).Select(c => c.PropD).SelectMany(c => c.Values.Select(v => v.Name)).Distinct().ToList();

//Combine to get a new list of columns 
    var columns = staticColumns.Union(dynamicColumns);

//Create object using columns

//Copy values from data to construct the new object.    
}

我需要逻辑方面的帮助来在运行时构造一个对象,该对象的结构如下,并且数据正确映射。

PropA    PropB    PropC  PropD11  PropD12  PropD21  PropD22
------------------------------------------------------------
A1       B1       C1     V11      V12       
A2       B2       C2                       V21      V22
A3       B3       C3              V31      V32  

【问题讨论】:

  • 你在寻求什么帮助?
  • 如何动态添加属性PropD11、PropD12、PropD21和PropD22,使动态对象看起来像表格?
  • 我不知道“动态对象”在这里的具体含义。如果您将 ExpandoObject 引用为 dynamic,则此处不合适,因为您的属性名称作为运行时字符串保存,而不是编译时符号。为此使用 ExpandoObject 是荒谬的。为什么不直接使用 Dictionary 或 DataTable?
  • @user1145404 啊,但这正是您在MyClass 中所拥有的,也是Dictionary&lt;string,object&gt; 的确切用途。这开始听起来像XY Problem。你真正想要完成什么?使用MyClass 这样做有什么限制?
  • 为什么它们需要是单独的属性?与仅在 PropD 中访问它们相比,这对您有什么好处?或者可能根据每个 NameValuePropD 更改为 Dictionary&lt;string,object&gt; 并忽略代码?

标签: c# dynamicobject


【解决方案1】:

假设您可能的属性名称是静态的,这里是一个答案类:

public class AnsClass {
    public string PropA { get; set; }
    public string PropB { get; set; }
    public string PropC { get; set; }

    public string PropD11 { get; set; }
    public string PropD12 { get; set; }
    public string PropD21 { get; set; }
    public string PropD22 { get; set; }
}

这里有一个 LINQ 语句来创建它们的列表:

var ans = data.Select(d => new AnsClass {
                    PropA = d.PropA,
                    PropB = d.PropB,
                    PropC = d.PropC,
                    PropD11 = d.PropD.Values.FirstOrDefault(v => v.Name == "PropD11")?.Value,
                    PropD12 = d.PropD.Values.FirstOrDefault(v => v.Name == "PropD12")?.Value,
                    PropD21 = d.PropD.Values.FirstOrDefault(v => v.Name == "PropD21")?.Value,
                    PropD22 = d.PropD.Values.FirstOrDefault(v => v.Name == "PropD22")?.Value,
                })
                .ToList();

如果PropD 的成员是动态的,您可以使用以下内容创建ExpandoObjects,但您最好还是直接使用MyClass - 它已经为此提供了最好的设计。

var ans2 = new List<dynamic>();
foreach (var d in data) {
    dynamic eo = new ExpandoObject();
    eo.PropA = d.PropA;
    eo.PropB = d.PropB;
    eo.PropC = d.PropC;
    var deo = (IDictionary<string,object>)eo;
    foreach (var dp in d.PropD.Values)
        deo.Add(dp.Name, dp.Value);
    ans2.Add(eo);
}

【讨论】:

  • 谢谢。但是,PropD 值是动态的。直到运行时我才知道这些名字。
  • @user1145404 我添加了代码来创建ExpandoObjects。
  • 谢谢。 ExpandoObject 的解决方案解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 2023-01-26
  • 2014-09-06
  • 2020-12-14
  • 1970-01-01
  • 2016-09-20
  • 2017-07-24
  • 1970-01-01
相关资源
最近更新 更多