Class TypeCache
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
providerITypeShapeProviderThe 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
Count
Gets the total number of entries in the cache.
public int Count { get; }
Property Value
DelayedValueFactory
Gets a factory method governing value initialization in case of recursive types.
public IDelayedValueFactory? DelayedValueFactory { get; init; }
Property Value
this[Type]
Gets or sets the value associated with the specified type.
public object? this[Type type] { get; set; }
Parameters
typeTypeThe 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
Provider
Gets the ITypeShapeProvider associated with the current cache.
public ITypeShapeProvider? Provider { get; }
Property Value
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
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
typeTypeThe key type.
Returns
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
typeShapeITypeShapeThe 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
typeTypeThe 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
Returns
TryGetValue(Type, out object?)
Attempts to get the value associated with the specified type.
public bool TryGetValue(Type type, out object? value)
Parameters
typeTypeThe type key whose value to get.
valueobjectWhen 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.