Is there a way in the C# standard API to lazily initialize a block of code?
I know Lazy<T>
but it is meant to initialize one variable, and requires a function that returns a T. I'd like something that's more like a LazyAction.
private _lazyInit = new LazyAction( () => {
...
// Do something big like get some data from DB
...
_myField1 = ...
_myField2 = ...
...
do no return stuff.
}
public object get_Field1() { _lazyInit.EnsureDone(); return _myfield1; }
public object get_Field2() { _lazyInit.EnsureDone(); return _myfield2; }
For thread-safety, the LazyAction would have some mechanism to ensure it's only run once.