nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
431 stars 50 forks source link

[Question] Is it possible to fill properties for dynamic object or for runtime type #42

Closed ma3yta closed 3 years ago

ma3yta commented 4 years ago

For example:

dynamic dynamicObj = new { Address = new { PostCode = 0 }, Name = string.Empty };
var faker = AutoFaker.Create(b=> {
    b.WithConventions();
});
faker.Fill(dynamicObj);
nickdodd79 commented 4 years ago

Hey @Ma3yTa

In your example, the compiler will create read only properties on your dynamic object. So based on this, you will not be able to fill a dynamic object. If you were to set your code up like the below (or similar), it will work, but I have identified a change that is required.

dynamic obj = new {};
obj.Name = string.Empty;
obj.Address = new {};
obj.Address.PostCode = 0;

I will get the changes in place and a new version published shortly.

nickdodd79 commented 3 years ago

Hey @Ma3yTa

I have released v2.11.0 which allows dynamic/ExpandoObject instances to be populated. This also includes nested dynamic/ExpandoObject properties.

Nick.

ma3yta commented 3 years ago

@nickdodd79 thank you!