ios – 如何将“method_getImplementation”和“method_setImplementation”移植到MonoTouch?

ios – 如何将“method_getImplementation”和“method_setImplementation”移植到MonoTouch?,第1张

概述从我的问题: Without subclassing a UIView or UIViewController: possible to catch if a subview was added? 我想知道如何从MonoTouch的答案中移植代码.它基本上用一个新方法替换一个方法,然后在没有子类化的情况下调用旧方法.是否有可能让这个指针在MonoTouch中工作? //Makes views an 从我的问题: Without subclassing a UIView or UIViewController: possible to catch if a subview was added?

我想知道如何从Monotouch的答案中移植代码.它基本上用一个新方法替换一个方法,然后在没有子类化的情况下调用旧方法.是否有可能让这个指针在Monotouch中工作?

//Makes vIEws announce their change of supervIEwsMethod method = class_getInstanceMethod([UIVIEw class],@selector(willMovetoSupervIEw:));IMP originalimp = method_getImplementation(method);voID (^block)(ID,UIVIEw*) = ^(ID _self,UIVIEw* supervIEw) {    [_self willChangeValueForKey:@"supervIEw"];    originalimp(_self,@selector(willMovetoSupervIEw:),supervIEw);    [_self dIDChangeValueForKey:@"supervIEw"];};IMP newImp = imp_implementationWithBlock((__brIDge voID*)block);method_setImplementation(method,newImp);
解决方法 这似乎是我们提供劫持方法的通用机制的一个很好的候选者.以下是纯C#代码的实现,您可以在此期间使用它:
[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> 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 uiVIEw)    {        Console.Writeline ("Moving to: {0}",Runtime.GetNSObject (uiVIEw));        original_impl (self,uiVIEw);        Console.Writeline ("Added");    }
总结

以上是内存溢出为你收集整理的ios – 如何将“method_getImplementation”和“method_setImplementation”移植到MonoTouch?全部内容,希望文章能够帮你解决ios – 如何将“method_getImplementation”和“method_setImplementation”移植到MonoTouch?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109624.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存