Table of Contents

Class GetOnce<TResult>

Namespace
ToSic.Lib.Helpers
Assembly
ToSic.Lib.Core.dll

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
GetOnce<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

bool

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

generator Func<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

log ILog

Log object to use when logging

generator Func<TResult>

Function which will generate the value on first use. The function must return the expected value/type.

timer bool

enable a timer from call/close

enabled bool

can be set to false if you want to disable logging

parameters string
message string
cPath string

auto pre filled by the compiler - the path to the code file

cName string

auto pre filled by the compiler - the method name

cLine int

auto 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

log ILog

Log object to use when logging

generator Func<ILog, TResult>

Function which will generate the value on first use. The function must return the expected value/type.

timer bool

enable a timer from call/close

enabled bool

can be set to false if you want to disable logging

parameters string
message string
cPath string

auto pre filled by the compiler - the path to the code file

cName string

auto pre filled by the compiler - the method name

cLine int

auto 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

log ILog

Log object to use when logging

generator Func<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.

timer bool

enable a timer from call/close

enabled bool

can be set to false if you want to disable logging

parameters string
message string
cPath string

auto pre filled by the compiler - the path to the code file

cName string

auto pre filled by the compiler - the method name

cLine int

auto 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

log ILog

Reset(TResult)

Reset the state and value so it will be re-generated next time it's needed.

public void Reset(TResult newValue)

Parameters

newValue TResult