mercredi 27 novembre 2019

How to set Lazy

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?





Aucun commentaire:

Enregistrer un commentaire