Hi @mc
You can try to create a ExpandoObject to add the property. Check the following code:
[HttpGet]
public IActionResult Get()
{
var data = new { Status = 1, Value = "x" };
//define a dictionary to store the property and the value.
Dictionary<string, object> dic = new Dictionary<string, object>();
//find original property from the anonymous class.
foreach (var pro in data.GetType().GetProperties())
{
dic.Add(pro.Name, pro.GetValue(data, null).ToString());
}
//create a ExpandoObject
var eo = new ExpandoObject();
var eoColl = (ICollection<KeyValuePair<string, object>>)eo;
//add the property and value into the expandoObject.
foreach (var kvp in dic)
{
eoColl.Add(kvp);
}
//add new property.
eoColl.Add(new KeyValuePair<string, object>("Code", "111"));
dynamic result = eoColl;
var testdata = result.Code;
return Ok(result);
}
The result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion