5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using MonoMac.ObjCRuntime;
29
using System.Runtime.InteropServices;
33
public static class ObjcHelper
35
[DllImport ("/usr/lib/libobjc.dylib")]
36
static extern bool class_addMethod (IntPtr cls, IntPtr sel, Delegate method, string argTypes);
38
[DllImport ("/usr/lib/libobjc.dylib")]
39
static extern IntPtr objc_getMetaClass (string name);
41
[DllImport ("/usr/lib/libobjc.dylib")]
42
static extern IntPtr class_getInstanceMethod (IntPtr cls, IntPtr sel);
44
[DllImport ("/usr/lib/libobjc.dylib")]
45
static extern IntPtr class_getClassMethod (IntPtr cls, IntPtr sel);
47
[System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
48
static extern bool method_exchangeImplementations (IntPtr m1, IntPtr m2);
50
[System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
51
static extern IntPtr method_setImplementations (IntPtr m1, Delegate impl);
53
[System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
54
static extern IntPtr object_getClass (IntPtr obj);
56
[System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
57
static extern IntPtr objc_getProtocol (string name);
59
[System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
60
static extern bool class_conformsToProtocol(IntPtr cls, IntPtr protocol);
62
[System.Runtime.InteropServices.DllImport ("/usr/lib/libobjc.dylib")]
63
static extern bool class_addProtocol(IntPtr cls, IntPtr protocol);
65
public static Class GetObjectClass (IntPtr obj)
67
return new Class (object_getClass (obj));
70
public static Class GetMetaClass (string name)
72
return new Class (objc_getMetaClass (name));
75
public static void InstanceMethodExchange (this Class cls, IntPtr sel1, IntPtr sel2)
77
IntPtr m1 = class_getInstanceMethod (cls.Handle, sel1);
78
if (m1 == IntPtr.Zero)
79
throw new Exception ("Class did not have a method for the first selector");
80
IntPtr m2 = class_getInstanceMethod (cls.Handle, sel2);
81
if (m2 == IntPtr.Zero)
82
throw new Exception ("Class did not have a method for the second selector");
83
if (!method_exchangeImplementations (m1, m2))
84
throw new Exception ("Failed to exchange implementations");
87
public static void MethodExchange (this Class cls, IntPtr sel1, IntPtr sel2)
89
IntPtr m1 = class_getClassMethod (cls.Handle, sel1);
90
if (m1 == IntPtr.Zero)
91
throw new Exception ("Class did not have a method for the first selector");
92
IntPtr m2 = cls.GetMethod (sel2);
93
if (m2 == IntPtr.Zero)
94
throw new Exception ("Class did not have a method for the second selector");
95
if (!method_exchangeImplementations (m1, m2))
96
throw new Exception ("Failed to exchange implementations");
99
public static IntPtr SetMethodImplementation (this Class cls, IntPtr method, Delegate impl)
101
IntPtr m1 = class_getClassMethod (cls.Handle, method);
102
if (m1 == IntPtr.Zero)
103
throw new Exception ("Class did not have a method for the first selector");
104
return method_setImplementations (m1, impl);
107
public static IntPtr GetMethod (this Class cls, IntPtr sel)
109
return class_getClassMethod (cls.Handle, sel);
112
public static bool AddMethod (this Class cls, IntPtr sel, Delegate method, string argTypes)
114
return class_addMethod (cls.Handle, sel, method, argTypes);
117
public static IntPtr GetProtocol (string name)
119
return objc_getProtocol (name);
122
public static bool ConformsToProtocol(this Class cls, IntPtr protocol)
124
return class_conformsToProtocol (cls.Handle, protocol);
127
public static bool AddProtocol(this Class cls, IntPtr protocol)
129
return class_addProtocol (cls.Handle, protocol);