You can't just add properties to compiled types. The only time that will work as properties (well, kind-of) is if you are using the dynamic
API, and the object is something that supports dynamic properties, for example:
dynamic obj = new ExpandoObject();
obj.Foo = 123;
obj.Bar = "abc";
however, those values are not available via reflection. An easier approach may be something that behaves as a dictionary:
var obj = new Dictionary<string,object>();
obj["Foo"] = 123;
obj["Bar"] = "abc";
there are more discoverable, via the dictionary API. Re your final question: when you do:
var o = new { foo = 1 };
the new { foo = 1 }
causes the compiler to generate an anonymous type, which is a regular C# class with read-only properties and an unpronounceable name. This can be referred to as object
(it is a regular class
, after all), but when you use var
the compiler uses the known type (with the horrible name), which gives you access to the properties (.foo
in this case).