From b48176613f597025fac53ed0bb28c0d737c5d812 Mon Sep 17 00:00:00 2001 From: Alex Corrado Date: Wed, 31 Jul 2013 17:24:49 -0400 Subject: [PATCH] Provide generic GetRootProxy methods for NSConnection This returns a proxy to a remote object, so we'll generally want it to return a binding typed as the remote object's interface. Runtime.GetNSObject sends the "class" message to determine the type of binding to create, but that always returns NSDistantObject in this case, so bypass it and explicitly create the type of binding we want. --- src/Foundation/NSConnection.cs | 64 ++++++++++++++++++++++++++++++++++ src/Makefile | 1 + src/foundation-desktop.cs | 12 +++---- 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 src/Foundation/NSConnection.cs diff --git a/src/Foundation/NSConnection.cs b/src/Foundation/NSConnection.cs new file mode 100644 index 000000000..6a5a200a2 --- /dev/null +++ b/src/Foundation/NSConnection.cs @@ -0,0 +1,64 @@ +// +// NSConnection.cs +// +// Author: +// Alex Corrado +// +// Copyright 2013 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System; + +using MonoMac.ObjCRuntime; + +namespace MonoMac.Foundation +{ + + public partial class NSConnection + { + + public TProxy GetRootProxy () where TProxy : NSObject + { + return GetRootProxy (_GetRootProxy ()); + } + + public static TProxy GetRootProxy (string name, string hostName) where TProxy : NSObject + { + return GetRootProxy (_GetRootProxy (name, hostName)); + } + + public static TProxy GetRootProxy (string name, string hostName, NSPortNameServer server) where TProxy : NSObject + { + return GetRootProxy (_GetRootProxy (name, hostName, server)); + } + + static TProxy GetRootProxy (IntPtr handle) where TProxy : NSObject + { + var result = Runtime.TryGetNSObject (handle) as TProxy; + + if (result == null) + result = (TProxy)Activator.CreateInstance (typeof (TProxy), new object[] { handle }); + + return result; + } + } +} \ No newline at end of file diff --git a/src/Makefile b/src/Makefile index 17fa22b77..e797b7133 100644 --- a/src/Makefile +++ b/src/Makefile @@ -48,6 +48,7 @@ CORE_SOURCES = $(EXTRA_CORE_SOURCES) \ MONOMAC_SOURCES = $(EXTRA_MONOMAC_SOURCES) \ ./Compat.cs \ ./Foundation/NSObjectMac.cs \ + ./Foundation/NSConnection.cs \ ./AppKit/EventArgs.cs \ ./AppKit/AppKitSynchronizationContext.cs \ ./AppKit/AppKitThreadAccessException.cs \ diff --git a/src/foundation-desktop.cs b/src/foundation-desktop.cs index 06d678574..40301f808 100644 --- a/src/foundation-desktop.cs +++ b/src/foundation-desktop.cs @@ -128,14 +128,14 @@ public interface NSConnection { [Static, Export ("connectionWithRegisteredName:host:usingNameServer:")] NSConnection LookupService (string name, [NullAllowed] string hostName, NSPortNameServer server); - [Export ("rootProxy")] - NSObject GetRootProxy (); + [Internal, Export ("rootProxy")] + IntPtr _GetRootProxy (); - [Static, Export ("rootProxyForConnectionWithRegisteredName:host:")] - NSObject GetRootProxy (string name, [NullAllowed] string hostName); + [Internal, Static, Export ("rootProxyForConnectionWithRegisteredName:host:")] + IntPtr _GetRootProxy (string name, [NullAllowed] string hostName); - [Static, Export ("rootProxyForConnectionWithRegisteredName:host:usingNameServer:")] - NSObject GetRootProxy (string name, [NullAllowed] string hostName, NSPortNameServer server); + [Internal, Static, Export ("rootProxyForConnectionWithRegisteredName:host:usingNameServer:")] + IntPtr _GetRootProxy (string name, [NullAllowed] string hostName, NSPortNameServer server); [Export ("remoteObjects")] NSObject [] RemoteObjects { get; }