ReflectionHelper
Provides utility methods for reflection-based operations including object cloning and field copying.
ReflectionHelper encapsulates common reflection-based functionality used throughout the Telerik WinControls framework for object manipulation, cloning, and field copying operations. It provides safe, efficient methods for copying object state while respecting serialization attributes and type constraints.
Key features include:
- Shallow cloning of objects with automatic field copying
- Selective field copying that respects NonSerialized attributes
- Delegate field exclusion to prevent event handler copying issues
- Integration with IRadCloneable interface for custom clone notifications
- Type-safe generic methods for compile-time type checking
This class is essential for creating object copies in scenarios like property inheritance, theme copying, and control state duplication where manual field-by-field copying would be error-prone and maintenance-intensive.
Definition
Namespace:Telerik.WinControls
Assembly:Telerik.WinControls.dll
Syntax:
public static class ReflectionHelper
Inheritance: objectReflectionHelper
Methods
Creates a new instance of the specified type and copies all compatible fields from the source object.
public static T Clone<T>(T source) where T : class, new()
The source object to clone.
Returns:T
A new instance of type T with field values copied from the source object.
This method performs a shallow clone operation by:
- Creating a new instance using the parameterless constructor
- Copying all compatible fields using CopyFields<T>(T, T)
- Notifying the clone if it implements IRadCloneable
The cloned object shares references to the same objects as the source for reference-type fields. This is a shallow copy, not a deep clone of the object graph.
If the cloned object implements IRadCloneable, both OnFieldsCopied() and OnCloneComplete() methods are called to allow for custom initialization logic.
Copies all compatible fields from a source object to a target object using reflection.
public static void CopyFields<T>(T target, T source) where T : class
The destination object that will receive the copied field values.
sourceTThe source object from which field values will be copied.
This method performs a shallow copy of all instance fields (public and non-public) from the source to the target object, with the following exclusions:
- Fields marked with the NonSerializedAttribute are skipped
- Delegate-type fields are skipped to prevent event handler copying issues
Reference type fields are copied by reference, meaning both objects will share the same reference to complex objects. This is a shallow copy operation, not a deep clone.
If the target object implements IRadCloneable, the OnFieldsCopied() method is called after the field copying process completes, allowing for custom post-copy logic.