Question by
Stals · Jul 07, 2016 at 10:35 AM ·
iosreflectionmethodaction
Weak System.Action iOS
I want to have a weak action that does not hold a strong reference to the target letting it destroy if needed. This code tries to achieve that.
public RequestObject(Action<MyClass> _callback) {
if (_callback != null) {
target = new WeakReference(_callback.Target);
method = _callback.Method;
}
}
System.Reflection.MethodInfo method;
WeakReference target = null;
public void Invoke(MyClass c) {
if (target != null && target.IsAlive)
{
var action = (Action<MyClass >)Delegate.CreateDelegate(typeof(Action<MyClass >), target.Target, method);
action.Invoke(e);
}
}
Is it event the correct way? Would it work on iOS? It uses MethodInfo from System.Reflection so I'm not sure. If this would not work on iOS, is there another way to convert Action and hold a weakref to its target?
Comment