我有以下 Xamarin.Mac 代码:
[Register("Swizzler")]
public class Swizzler : NSObject
{
[DllImport("/usr/lib/libobjc.dylib")] public static extern IntPtr class_getInstanceMethod(IntPtr classHandle, IntPtr Selector);
[DllImport("/usr/lib/libobjc.dylib")] public static extern bool method_exchangeImplementations(IntPtr m1, IntPtr m2);
public void AttemptSwizzle()
{
var swizzledClassPtr = Class.GetHandle("Swizzled");
var swizzlerClassPtr = Class.GetHandle("Swizzler");
SwizzleInstanceMethod(swizzledClassPtr, new Selector("originalMethod"), swizzlerClassPtr, new Selector("newMethod"));
var swizzled = new Swizzled();
swizzled.PerformSelector(new Selector("originalMethod"));
}
internal void SwizzleInstanceMethod(IntPtr originalClassPtr, Selector originalSelector, IntPtr newClassPtr, Selector newSelector)
{
var originalMethod = class_getInstanceMethod(originalClassPtr, originalSelector.Handle);
var swizzledMethod = class_getInstanceMethod(newClassPtr, newSelector.Handle);
method_exchangeImplementations(originalMethod, swizzledMethod);
}
[Export("newMethod")]
public void NewMethod()
{
Console.WriteLine("New method called");
}
}
[Register("Swizzled")]
internal class Swizzled : NSObject
{
[Export("originalMethod")]
public void OriginalMethod()
{
Console.WriteLine("Original method called");
}
}
https://github.com/alataffective/XamarinSwizzler上的代码示例。
调用时,new Swizzler().AttemptSwizzle()
我得到以下输出:
SomeMethod called
也就是说,混合没有发生。为什么不?