FlexObject is a hybrid object which combines the best of typed and dynamic in .NET
You can simply use it as a dynamic object
dynamic obj = new FlexObject();
obj.x = 3;
obj.y = "yo";
You can use it as a typed object
public class Car : FlexObject
{
public string Name { get;set;}
}
Car car = new Car();
car.Name = "test";
car.Age = 15;
if (car.Name == "test" && car["Age"] == 15)
{
}
You can use it as a typed object AND a dynamic object at the same time!
Car car = new Car();
if (car.Name == null)
{
// use typed property
car.Name = "Volvo";
// assign a property via dictionary syntax
car["x"] = 3;
// use a property using dynamic keyword
dynamic car2 = car;
if (car.x == 3 && car.Name == "Volvo")
{
}
}
You can also enumerate typed and untyped properties the same way.
foreach(var property in car.GetProperties())
{
Debug.Print(car[property]);
}
It also supports INotifyPropertyChanged notifications.
public class Car : FlexObject
{
private string _name;
public string Name { get=>_name; set { _name = value; NotifyChanged();}}
}
Car car = new Car();
car.PropertyChanged += (sender, args) => Debug.Print("Property {0} changed", args.PropertyName);
car.Name = "Volvo";
// => Property Name Changed
car["x"] = 3;
// => Property x Changed