Table of Contents

Class TypeGenerationContext

Namespace
PolyType.Utilities
Assembly
PolyType.dll

Defines a thread-local context for generating values requiring type graph traversal.

public sealed class TypeGenerationContext : IReadOnlyDictionary<Type, object?>, IReadOnlyCollection<KeyValuePair<Type, object?>>, IEnumerable<KeyValuePair<Type, object?>>, IEnumerable, ITypeShapeFunc
Inheritance
TypeGenerationContext
Implements
Inherited Members

Constructors

TypeGenerationContext()

Initializes a new instance of the TypeGenerationContext class.

public TypeGenerationContext()

Properties

Count

Gets the number of entries in the local context.

public int Count { get; }

Property Value

int

DelayedValueFactory

Gets a factory method governing value initialization in case of recursive types.

public IDelayedValueFactory? DelayedValueFactory { get; init; }

Property Value

IDelayedValueFactory

this[Type]

Gets the value associated with the specified type.

public object? this[Type type] { get; }

Parameters

type Type

The type to look up.

Property Value

object

The value associated with the specified key.

ParentCache

Gets the parent cache to which the completed results can eventually be committed.

public TypeCache? ParentCache { get; }

Property Value

TypeCache

ValueBuilder

Gets a factory method governing the creation of values when invoking the GetOrAdd(ITypeShape, object?) method.

public ITypeShapeFunc? ValueBuilder { get; init; }

Property Value

ITypeShapeFunc

Methods

Add(Type, object?, bool)

Adds a new entry to the dictionary, completing any delayed values for the key type.

public void Add(Type key, object? value, bool overwrite = false)

Parameters

key Type

The key type of the new entry.

value object

The value of the new entry.

overwrite bool

Whether to overwrite existing entries.

Clear()

Clears the local cache.

public void Clear()

ContainsKey(Type)

Determines whether the cache contains a value for the specified type.

public bool ContainsKey(Type type)

Parameters

type Type

The key type.

Returns

bool

true is found, or false otherwise.

GetOrAdd(ITypeShape, object?)

Gets or adds a value keyed on the type represented by typeShape.

public object? GetOrAdd(ITypeShape typeShape, object? state = null)

Parameters

typeShape ITypeShape

The type shape representing the key type.

state object

The state object to be passed to the visitor.

Returns

object

The final computed value.

TryCommitResults()

Attempts to commit the results of the generation context to the parent cache.

public bool TryCommitResults()

Returns

bool

true if all values were committed successfully, or false if no values were committed due to conflicts.

Remarks

In case of a failed commit operation due to conflicts, the current results should be discarded and retried.

Exceptions

InvalidOperationException

Instance does not specify a ParentCache.

TryGetValue(ITypeShape, out object?)

Looks up the value for the specified ITypeShape.

public bool TryGetValue(ITypeShape typeShape, out object? value)

Parameters

typeShape ITypeShape

The type shape representing the key type.

value object

The value returned by the lookup operation.

Returns

bool

True if either a completed or delayed value have been returned.