This looks like a good candidate for us to provide a general purpose mechanism to hijack methods. Here is an implementation in pure C# code that you can use in the meantime:
[DllImport ("/usr/lib/libobjc.dylib")]
extern static IntPtr class_getInstanceMethod (IntPtr classHandle, IntPtr Selector);
[DllImport ("/usr/lib/libobjc.dylib")]
extern static Func<IntPtr,IntPtr,IntPtr> method_getImplementation (IntPtr method);
[DllImport ("/usr/lib/libobjc.dylib")]
extern static IntPtr imp_implementationWithBlock (ref BlockLiteral block);
[DllImport ("/usr/lib/libobjc.dylib")]
extern static void method_setImplementation (IntPtr method, IntPtr imp);
static Func<IntPtr,IntPtr,IntPtr> original_impl;
void HijackWillMoveToSuperView ()
{
var method = class_getInstanceMethod (new UIView ().ClassHandle, new Selector ("willMoveToSuperview:").Handle);
original_impl = method_getImplementation (method);
var block_value = new BlockLiteral ();
CaptureDelegate d = MyCapture;
block_value.SetupBlock (d, null);
var imp = imp_implementationWithBlock (ref block_value);
method_setImplementation (method, imp);
}
delegate void CaptureDelegate (IntPtr block, IntPtr self, IntPtr uiView);
[MonoPInvokeCallback (typeof (CaptureDelegate))]
static void MyCapture (IntPtr block, IntPtr self, IntPtr uiView)
{
Console.WriteLine ("Moving to: {0}", Runtime.GetNSObject (uiView));
original_impl (self, uiView);
Console.WriteLine ("Added");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…