Simplest C# code to poll a property?

后端 未结 8 1913
慢半拍i
慢半拍i 2021-01-03 06:31

I would like to know the simplest code for polling a property\'s value, to execute the code in its getter.
Currently I\'m using: instance.property.ToString();

8条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 07:08

    I like Jon Skeet's answer, but along the same vein, this would also work without having to write an extension method:

    GC.KeepAlive(instance.SomeProperty);
    

    The KeepAlive method accepts an object and does nothing with it, like the NoOp extension method.

提交回复
热议问题