Class GetOnce<TResult>
Simple helper class to use on object properties which should be generated once.
Important for properties which can also return null, because then checking for null won't work to determine if we already tried to retrieve it.
[InternalApi_DoNotUse_MayChangeWithoutNotice]
public class GetOnce<TResult>
Type Parameters
TResult
- Inheritance
-
objectGetOnce<TResult>
Constructors
GetOnce()
Construct an empty GetOnce object for use later on.
In case you're wondering why we can't pass the generator in on the constructor:
Reason is that in most cases we need real objects in the generator function,
which doesn't work in a static context.
This means that if the = new GetOnce() is run on the private property
(which is the most common case) most generators can't be added.
public GetOnce()
Properties
IsValueCreated
Determines if value has been created.
The name IsValueCreated is the same as in a Lazy() object
public bool IsValueCreated { get; }
Property Value
Methods
Get(Func<TResult>)
Get the value. If not yet retrieved, use the generator function (but only once).
public TResult Get(Func<TResult> generator)
Parameters
generatorFunc<TResult>Function which will generate the value on first use.
Returns
- TResult
Get(ILog, Func<TResult>, bool, bool, string, string, string, string, int)
Getter with will log it's actions when it retrieves the property the first time.
public TResult Get(ILog log, Func<TResult> generator, bool timer = false, bool enabled = true, string parameters = null, string message = null, string cPath = null, string cName = null, int cLine = 0)
Parameters
logILogLog object to use when logging
generatorFunc<TResult>Function which will generate the value on first use. The function must return the expected value/type.
timerboolenable a timer from call/close
enabledboolcan be set to false if you want to disable logging
parametersstringmessagestringcPathstringauto pre filled by the compiler - the path to the code file
cNamestringauto pre filled by the compiler - the method name
cLineintauto pre filled by the compiler - the code line
Returns
- TResult
GetL(ILog, Func<ILog, TResult>, bool, bool, string, string, string, string, int)
Getter with will log when it gets the property the first time. This will also provide the logger to the generator as a first function parameter.
public TResult GetL(ILog log, Func<ILog, TResult> generator, bool timer = false, bool enabled = true, string parameters = null, string message = null, string cPath = null, string cName = null, int cLine = 0)
Parameters
logILogLog object to use when logging
generatorFunc<ILog, TResult>Function which will generate the value on first use. The function must return the expected value/type.
timerboolenable a timer from call/close
enabledboolcan be set to false if you want to disable logging
parametersstringmessagestringcPathstringauto pre filled by the compiler - the path to the code file
cNamestringauto pre filled by the compiler - the method name
cLineintauto pre filled by the compiler - the code line
Returns
- TResult
GetM(ILog, Func<ILog, (TResult Result, string Message)>, bool, bool, string, string, string, string, int)
Getter with will log when it gets the property the first time. It provides the logger as a first function in the parameter.
public TResult GetM(ILog log, Func<ILog, (TResult Result, string Message)> generator, bool timer = false, bool enabled = true, string parameters = null, string message = null, string cPath = null, string cName = null, int cLine = 0)
Parameters
logILogLog object to use when logging
generatorFunc<ILog, (TResult Result, string Message)>Function which will generate the value on first use. The function must return a Tuple
(TResult Result, string Message)which it uses to log when done.timerboolenable a timer from call/close
enabledboolcan be set to false if you want to disable logging
parametersstringmessagestringcPathstringauto pre filled by the compiler - the path to the code file
cNamestringauto pre filled by the compiler - the method name
cLineintauto pre filled by the compiler - the code line
Returns
- TResult
Reset(ILog)
Reset the state and value so it will be re-generated next time it's needed.
public void Reset(ILog log = null)
Parameters
logILog
Reset(TResult)
Reset the state and value so it will be re-generated next time it's needed.
public void Reset(TResult newValue)
Parameters
newValueTResult