So I have a static method that returns a compiled string. The method gets an anonymous object:
var data = new {one = 12.334, two = 235, three = {23, 34, 43}};
And the static method Result should iterate through each value and round the decimals up with:
Math.Round(num, 1);
But I can't seem to find a way to iterate the anonymous object. I want the object to be of type dynamic for easy access when writing to console, but I don't seem to be able to iterate the dynamic object either. I need a way to round up all the values of type double.
public static string Result(object data)
{
dynamic x = data;
// Iterate through each value and MathRound HERE
string compiled = $"test1: {x.one};test2: {x.two};test3: {x.three}";
return compiled;
}
Foreach-method with the dynamic object just throws this error:
Cannot implicitly convert type '<>f__
AnonymousType0<double,int,int[]>' to 'System.Collections.IEnumerable'
And foreach-method with the anonymous object throws this:
foreach statement cannot operate on variables of type 'object' because 'object'
does not contain a public definition for 'GetEnumerator
I'm a new learner of C#, so I apologize if the terminology is unclear. Do anyone know how to fix this?