Table of Contents

Class TypeCache

Namespace
PolyType.Utilities
Assembly
PolyType.dll

Defines a thread-safe cache that keys values on ITypeShape instances.

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

Remarks

Facilitates workflows common to generating values during type graph traversal, including support delayed value creation in case of recursive types.

Constructors

TypeCache(ITypeShapeProvider?)

Initializes a new instance of the TypeCache class.

public TypeCache(ITypeShapeProvider? provider = null)

Parameters

provider ITypeShapeProvider

The shape provider associated with the current cache.

Properties

CacheExceptions

Gets a value indicating whether exceptions should be cached.

public bool CacheExceptions { get; init; }

Property Value

bool

Count

Gets the total number of entries in the cache.

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 or sets the value associated with the specified type.

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

Parameters

type Type

The type to look up.

Property Value

object

The value associated with the specified key.

MultiProviderCache

Gets the global cache to which this instance belongs.

public MultiProviderTypeCache? MultiProviderCache { get; }

Property Value

MultiProviderTypeCache

Provider

Gets the ITypeShapeProvider associated with the current cache.

public ITypeShapeProvider? Provider { get; }

Property Value

ITypeShapeProvider

ValueBuilderFactory

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

public Func<TypeGenerationContext, ITypeShapeFunc>? ValueBuilderFactory { get; init; }

Property Value

Func<TypeGenerationContext, ITypeShapeFunc>

Remarks

This factory takes a newly created TypeGenerationContext to construct an ITypeShapeFunc that is responsible for generating the value associated with a given type shape. The generation context wraps the created ITypeShapeFunc and can be used to recursively look up and cache values for nested types, including handling potentially cyclic type graphs.

Because the generation context implements ITypeShapeFunc, this factory can effectively be seen as a Func<ITypeShapeFunc, ITypeShapeFunc> where the resultant function is being passed a reference to itself for the purpose of handling recursive calls. This makes it a specialized form of the Y-combinator.

Methods

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.

CreateGenerationContext()

Creates a new TypeGenerationContext instance for the cache.

public TypeGenerationContext CreateGenerationContext()

Returns

TypeGenerationContext

A new TypeGenerationContext instance for the cache.

GetOrAdd(ITypeShape)

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

public object? GetOrAdd(ITypeShape typeShape)

Parameters

typeShape ITypeShape

The type shape representing the key type.

Returns

object

The final computed value.

GetOrAdd(Type)

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

public object? GetOrAdd(Type type)

Parameters

type Type

The key type.

Returns

object

The final computed value.

TryAdd(Type, object?)

Attempts to add the value associated with the specified type to the cache.

public bool TryAdd(Type type, object? value)

Parameters

type Type

The type associated with the value.

value object

The value to attempt to add.

Returns

bool

true if the value was added successfully, false otherwise.

TryGetValue(Type, out object?)

Attempts to get the value associated with the specified type.

public bool TryGetValue(Type type, out object? value)

Parameters

type Type

The type key whose value to get.

value object

When this method returns, contains the value associated with the specified type, if the type is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

Returns

bool

true if the cache contains an element with the specified type; otherwise, false.