Suppose all the dependencies have already been registered at the beginning of the program. At later points in the program how can you use AutoFac to create a new object with a parameterless constructor and inject the registered properties into the object?
You can register your object in the container with PropertiesAutowired
then use resolve when you need an instace:
ContainerBuilder builder = new ContainerBuilder();
// builder register other dependencies
builder.RegisterType<MyObject>().PropertiesAutowired();
var container = builder.Build();
var myObject = container.Resolve<MyObject>(); //the properties will be filled
Or if you don't want to register in the container your can use the InjectProperties
method on an existing instance:
ContainerBuilder builder = new ContainerBuilder();
// builder register other dependencies
var container = builder.Build();
var myObject = new MyObject();
container.InjectProperties(myObject); //the properties will be filled
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With