Click or drag to resize
WeakActionT Class
Represents an action delegate which contains a WeakReference so the GarbageCollector can remove the reference if its not needed anymore.
Inheritance Hierarchy
SystemObject
  DW.SharpToolsWeakActionT

Namespace: DW.SharpTools
Assembly: DW.SharpTools (in DW.SharpTools.dll) Version: 2.4.0.0 (2.4.0.0)
Syntax
C#
public class WeakAction<T>

Type Parameters

T
The type of the parameter of the method that this delegate encapsulates. This type parameter is contravariant. That is, you can use either the type you specified or any type that is less derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.

The WeakActionT type exposes the following members.

Constructors
Methods
  NameDescription
Public methodGetAction
Returns the in the WeakAction stored action delegate if its still alive.
Public methodHasAction
Checks if the current WeakAction contains a specific action delegate.
Top
Properties
  NameDescription
Public propertyIsAlive
Gets the information if the action delegate target object is still there or removed by the GarbageCollector.
Top
Examples
C#
private class MainViewModel
{
    private Other _other;
    private WeakAction<string> _otherAction;

    public void CreateOtherAction(string parameter)
    {
        _other = new Other();
        _otherAction = new WeakAction<string>(new Action<string>(s => _other.Do(parameter)));
    }

    public void CleanOther()
    {
        _other = null;
    }

    public void ExecuteOther(string parameter)
    {
        if (_otherAction == null || !_otherAction.IsAlive)
            throw new InvalidOperationException("Please call CreateOtherAction first.");

        _otherAction.GetAction().Invoke(parameter);
    }
}

private class Other
{
    public void Do(string parameter)
    {
    }
}
See Also