Method ChangeProperty
ChangeProperty<T>(ref T, T, Func<ValueChangingEventArgs<T>, bool>?, EventHandler<ValueChangingEventArgs<T>>?, Action<T>, Action<ValueChangedEventArgs<T>>?, EventHandler<ValueChangedEventArgs<T>>?, out T)
Executes a CWP workflow for a property change, with pre- and post-change events.
public static bool ChangeProperty<T>(ref T currentValue, T newValue, Func<ValueChangingEventArgs<T>, bool>? onChanging, EventHandler<ValueChangingEventArgs<T>>? changingEvent, Action<T> doWork, Action<ValueChangedEventArgs<T>>? onChanged, EventHandler<ValueChangedEventArgs<T>>? changedEvent, out T finalValue)
Parameters
currentValueTReference to the current property value, which may be null for nullable types. If the change is not cancelled, this will be set to
finalValue.newValueTThe proposed new property value, which may be null for nullable types.
onChangingFunc<ValueChangingEventArgs<T>, bool>The virtual method invoked before the change, returning true to cancel.
changingEventEventHandler<ValueChangingEventArgs<T>>The pre-change event raised to allow modification or cancellation.
doWorkAction<T>The action that performs the actual work of setting the property (e.g., updating backing field, calling related methods).
onChangedAction<ValueChangedEventArgs<T>>The virtual method invoked after the change.
changedEventEventHandler<ValueChangedEventArgs<T>>The post-change event raised to notify of the completed change.
finalValueTThe final value after the workflow, reflecting any modifications, which may be null for nullable types.
Returns
- bool
True if the property was changed, false if cancelled.
Type Parameters
TThe type of the property value, which may be a nullable reference type (e.g., string ?).
Examples
string? current = _schemeName;
string? proposed = "Base";
Func<ValueChangingEventArgs<string?>, bool> onChanging = OnSchemeNameChanging;
EventHandler<ValueChangingEventArgs<string?>>? changingEvent = SchemeNameChanging;
Action<string?> doWork = value => _schemeName = value;
Action<ValueChangedEventArgs<string?>>? onChanged = OnSchemeNameChanged;
EventHandler<ValueChangedEventArgs<string?>>? changedEvent = SchemeNameChanged;
bool changed = CWPPropertyHelper.ChangeProperty(
current, proposed, onChanging, changingEvent, doWork, onChanged, changedEvent, out string? final);
Exceptions
- InvalidOperationException
Thrown if NewValue is null for non-nullable reference types after the workflow.