I'm trying to collate the properties from an existing object that contains some static properties. The object also contains a Dictionary with key value pairs. These key value pairs should be dynamically added as properties to the new object I'm creating.
This is the set up for source object:
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; }
}
This is where I create a list of the objects:
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.
}
I need help with logic to construct an object at runtime, that has the structure as below and the data correctly mapped.
PropA PropB PropC PropD11 PropD12 PropD21 PropD22
------------------------------------------------------------
A1 B1 C1 V11 V12
A2 B2 C2 V21 V22
A3 B3 C3 V31 V32