Table of Contents

Class GetOnce<TResult>

Namespace
ToSic.Lib.Helpers
Assembly
ToSic.Sys.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
object
GetOnce<TResult>

Remarks

Constructor is empty.

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.

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

Reset()

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

public void Reset()

Reset(ILog?)

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

public void Reset(ILog? log)

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