Class ParserExecutionContextCopier<TContext>
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
TContextThe 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
sourceTContextThe context instance whose state is copied.
factoryFunc<TContext>The caller-provided factory used to create the target context instance when
sourcedoes not implement ICloneable.
Returns
- TContext
A new context instance containing the copied state.
Exceptions
- ArgumentNullException
sourceis null, orfactoryis 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
sourceTContextThe context instance whose state is copied.
targetTContextThe 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
sourceortargetis null.- InvalidOperationException
The context type contains an unsupported readonly instance field.