Table of Contents

Class ParserExecutionContextCopier<TContext>

Namespace
Utils.Parser.Runtime
Assembly
Utils.Parser.dll

Copies parser execution-context instances with a cached, compiled field-copy delegate for each context type.

public static class ParserExecutionContextCopier<TContext> where TContext : class

Type Parameters

TContext

The parser execution-context type to copy.

Inheritance
ParserExecutionContextCopier<TContext>
Inherited Members

Remarks

The copier performs a shallow structural copy intended for parser execution contexts. It recreates known collection containers such as arrays, List<T>, Dictionary<TKey, TValue>, and HashSet<T> with explicit copy expressions, and can recreate unknown IEnumerable<T> collections when they expose a safe copy constructor, parameterless constructor with AddRange(IEnumerable<T>), or parameterless constructor with Add(T). It does not deep-clone contained elements. Fields whose declared type implements ICloneable are copied by calling Clone() on the field value so that objects expressing their own state-copy contract are respected. Unknown references and collections without a safe reconstruction strategy are copied by reference. When Copy(TContext, Func<TContext>) receives a source context that implements ICloneable, that clone contract takes priority and the supplied factory is not invoked. Static fields and field-like event backing fields are ignored. Readonly instance fields are rejected because they cannot be assigned safely during normal field-copy operations. Reflection is used only while building the delegate cached by the closed generic type; subsequent field-copy operations invoke the compiled delegate.

Field-copy strategy is primarily selected from the declared field type. Concrete known collections and common collection interfaces are copied structurally. Sequential interfaces (IEnumerable<T>, ICollection<T>, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T>) are copied into a new List<T> assignable to the declared type. Dictionary interfaces (IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>) and set interfaces (ISet<T>, IReadOnlySet<T>) are copied into a new Dictionary<TKey, TValue> or HashSet<T>. For these interfaces, a runtime type check is used solely to preserve the source comparer when the stored value is the standard concrete container; otherwise a standard copy constructor is used. Fields whose declared type is not a recognized collection interface and does not implement ICloneable use the unknown-collection fallback or are copied by reference.

Methods

Copy(TContext, Func<TContext>)

Creates a new context instance by using Clone() when available, or through factory followed by field copying.

public static TContext Copy(TContext source, Func<TContext> factory)

Parameters

source TContext

The context instance whose state is copied.

factory Func<TContext>

The caller-provided factory used to create the target context instance when source does not implement ICloneable.

Returns

TContext

A new context instance containing the copied state.

Exceptions

ArgumentNullException

source is null, or factory is null when field copying is required.

InvalidOperationException

Clone() returns null, returns a value that is not assignable to TContext, the factory returns null, or the context type contains an unsupported readonly instance field during field copying.

CopyTo(TContext, TContext)

Copies the state of source into an existing target context instance through field copying.

public static void CopyTo(TContext source, TContext target)

Parameters

source TContext

The context instance whose state is copied.

target TContext

The context instance that receives the copied state.

Remarks

This method intentionally does not use ICloneable because callers provide the target instance that must receive the copied fields.

Exceptions

ArgumentNullException

source or target is null.

InvalidOperationException

The context type contains an unsupported readonly instance field.