-2

I have two different lists. I want to combine these list in an object. I don't know the property names in these lists, which is why I cannot create a class in advance.

public class exampleConfigProperty
{
  public string PropertName { get; set; };
  public string PropertValue { get; set; };
}

public class exampleFile
{
  public string config1 { get; set; };
}

List<exampleConfigProperty> myList = new List<exampleConfigProperty>();
myList.add(new exampleConfigProperty { 
  PropertName = "Config_Property_Name", 
  PropertValue = "Config Property Value" 
});

List<exampleFile> myListTwo = new List<exampleFile>();
myListTwo.add(new exampleFile {
  config1 = "Config Value" 
});

I tried to figure out if is there any way I can use the code below:

var obj = new object();

foreach(var item in myList)
{
  obj.addProperty(item.PropertName, item.PropertValue );
}

foreach(var item in myListTwo)
{
  obj.addProperty(nameof(item.config1), item.config1 );
}

In conclusion I try to create an object instance by using two lists, for example:

var resut = new { 
  Config_Property_Name = "Config Property Value", 
  config1 ="Config Value" 
}

dynamic type does not work so what should I do to get an object instance by using two lists?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Sinan Bay
  • 171
  • 4
  • 12
  • `dynamic` sounds like it's quite likely to meet your needs. When you say it "not work", what exactly do you mean? What did you try, and what went wrong? It would think it will be a more appropriate solution then whatever your'e trying to do in the code above (whose aim is not entirely clear to me). – ADyson Jun 10 '21 at 14:25
  • 2
    Sounds like you might want to use a dictionary of type `Dictionary` – NotFound Jun 10 '21 at 14:35
  • 1
    While it is technically possible to create a dynamic object with dynamic property names, the real problem arises when you want to consume these properties, precisely because you don’t know which property name to use to access the property. The dictionary approach proposed by @404 is the way to go, where the property name is used as key in the dictionary. This allows you to get a value through a property name given in a string variable. – Olivier Jacot-Descombes Jun 10 '21 at 14:38
  • to sum up , in dynamic i cannot obtain the object directly. There are different classes in dynamic. Actually i tried to create a class and then i want to get a instance from that class.But the class's property names is the value of a different property value. exampleConfigProperty.PropertName property value is the propery name of the class which i'll create for the instance . By the way the value of the property which i create should be exampleConfigProperty.PropertyValue – Sinan Bay Jun 10 '21 at 14:43
  • 2
    This desire is almost always the result of a mistake made much earlier in the design process... a missing Interface, for example. – Joel Coehoorn Jun 10 '21 at 14:45
  • İts not a mistake. I want to create the class like a json object. – Sinan Bay Jun 10 '21 at 14:55
  • 1
    If you only want to make JSON from it (and no other tasks), you can build a dynamic structure using newtonsoft's JObject and related classes, for example. – ADyson Jun 10 '21 at 14:58
  • Okay, let's assume that you managed to create such an object dynamically with property names you have read from a configuration file, not known at compile time. Now you want to read a configuration value `var value = obj.PropertyName`. Which property name are you using in your code? Either you know the name and you can as well create a class having such a property (in fact with all the configuration properties) OR you don't know the name and you cannot write down this piece of code! – Olivier Jacot-Descombes Jun 10 '21 at 15:07
  • Does this answer your question? [Dynamically Add C# Properties at Runtime](https://stackoverflow.com/questions/15819720/dynamically-add-c-sharp-properties-at-runtime) and [Dynamically add properties to a existing object](https://stackoverflow.com/questions/6329489/dynamically-add-properties-to-a-existing-object) and [How Can I add properties to a class on runtime in C#?](https://stackoverflow.com/questions/14724822/how-can-i-add-properties-to-a-class-on-runtime-in-c) –  Jun 10 '21 at 16:13

1 Answers1

0

Since you just want to make a JSON I think would fit your criteria:

var test = new Dictionary<string, string>(); //<string, object> for mixed types of values

test["key1"] = "value1";
test["key2"] = "value2";

var result = JsonConvert.SerializeObject(test); //indented for readability of result

// result:
// {
//   "key1": "value1",
//   "key2": "value2"
// }
NotFound
  • 5,005
  • 2
  • 13
  • 33