Current responses do not simulate exactly what happens with JS. It can even achieve a similar (and not equal) result, but in a very different way. So it simulates correctly:
using static System.Console;
using System.Dynamic;
public class Program {
public static void Main() {
dynamic obj = new ExpandoObject();
obj.propriedade1 = "valor1";
obj.propriedade2 = "valor2";
WriteLine(obj.propriedade1);
WriteLine(obj.propriedade2);
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Documentation of ExpandoObject()
.
This way you can add and remove members in the object as you can in JS. You can manipulate everything dynamically as it is in JS, with a standard syntax of the language. Thus the object is created without having a class as a model, but it behaves as if it were a normal object of . NET.
It may not be necessary for the AP, but the premise of the question indicates this.
Well @bigown in terms of solution, I had used Fernando’s tip. But with his answer now I could understand more deeply the concept. More important than making it work is to know why it’s working... and also use the best solution. Thank you!
– Jedaias Rodrigues