I searched my brains out and wasn't able to find a suitable answer, so here we go! I have the following property in each of my forms or objects:
private RootWorkItem m_RootWorkItem;
[RootWorkItem(Inject = true)]
public RootWorkItem RootWorkItem {
get {
if (m_RootWorkItem == null) {
m_RootWorkItem = new RootWorkItem();
}
return m_RootWorkItem;
}
}
Note that RootWorkItem is a class I have designed that contains a collection of Services (Collection). Basically, all my services are loaded from external DLL files using reflection and Activator.CreateInstance, and put into RootWorkItem.Services.
But how on earth can I actually "inject" the very first RootWorkItem created into every other RootWorkItem marked with my attribute in my program?
How do these DI patterns ACTUALLY "DO" the injection "into" a property? I know the concepts of DI but I am trying to write my own, extremely basic DI for only one single object. I've been messing with reflection tons to try and find some suitable way to do this and I'm not even closed to coming up with a right way to do it.
Edited for more clearer question: How does a DI framework/service know when an object is created that contains the property to inject?
ie:
class MyForm : Form {
[RootWorkItem(Inject = true)]
public RootWorkItem RootWorkItem { get; set; }
end class
class Program {
Main {
MyForm form = new MyForm();
form.Show();
}
end class
How does it know to inject the property value when the form is created, if nothing notifies the service that it was created or it needs injected?