Class TypeGenerationContext
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
DelayedValueFactory
Gets a factory method governing value initialization in case of recursive types.
public IDelayedValueFactory? DelayedValueFactory { get; init; }
Property Value
this[Type]
Gets the value associated with the specified type.
public object? this[Type type] { get; }
Parameters
typeTypeThe 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
ValueBuilder
Gets a factory method governing the creation of values when invoking the GetOrAdd(ITypeShape, object?) method.
public ITypeShapeFunc? ValueBuilder { get; init; }
Property Value
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
keyTypeThe key type of the new entry.
valueobjectThe value of the new entry.
overwriteboolWhether 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
typeTypeThe key type.
Returns
GetOrAdd(ITypeShape, object?)
Gets or adds a value keyed on the type represented by typeShape.
public object? GetOrAdd(ITypeShape typeShape, object? state = null)
Parameters
typeShapeITypeShapeThe type shape representing the key type.
stateobjectThe 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
typeShapeITypeShapeThe type shape representing the key type.
valueobjectThe value returned by the lookup operation.
Returns
- bool
True if either a completed or delayed value have been returned.