I'm trying to do somethings have three things:
- Property is auto setter/getter (required)
- Property is lazy loading (required)
How can I have lazy loading for C# automatic properties?
To make two things above, I think I need to use Reflection (or maybe other). This is what I have tried so far. I have a object test:
public class MyClass
{
private Lazy<MyObjectClass> lazyObjectClass = new Lazy<MyObjectClass>(() => new MyObjectClass());
public MyObjectClass MyObject { get { return lazyObjectClass.Value; } }
}
Normally, the MyObjectClass will be initialize when MyObject called.
But, I want to auto-initialize MyObjectClass. So, I change the MyClass to like this:
public class MyClass
{
public MyClass()
{
//Get field
var field = this.GetType().GetField("lazyObjectClass", BindingFlags.NonPublic | BindingFlags.Instance);
//Get value of field
Lazy<MyObjectClass> lazyValue = (Lazy<MyObjectClass>)field.GetValue(this);
//Get property MyObject
var property = this.GetType().GetProperty("MyObject");
//Set value to the MyObject
property.SetValue(this, lazyValue.Value); // <- when It called, MyObjectClass is ititialize too
}
private Lazy<MyObjectClass> lazyObjectClass = new Lazy<MyObjectClass>(() => new MyObjectClass());
public MyObjectClass MyObject { get; set; }
}
But, when I SetValue to MyObject by Reflection, MyObjectClass is initialize too. How can I fix that or let me know if I should use a totally different solution.