Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Provide generic GetRootProxy methods for NSConnection
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Alex Corrado committed Jul 31, 2013
1 parent b44c40b commit b481766
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
64 changes: 64 additions & 0 deletions src/Foundation/NSConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// NSConnection.cs
//
// Author:
// Alex Corrado <[email protected]>
//
// 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<TProxy> () where TProxy : NSObject
{
return GetRootProxy<TProxy> (_GetRootProxy ());
}

public static TProxy GetRootProxy<TProxy> (string name, string hostName) where TProxy : NSObject
{
return GetRootProxy<TProxy> (_GetRootProxy (name, hostName));
}

public static TProxy GetRootProxy<TProxy> (string name, string hostName, NSPortNameServer server) where TProxy : NSObject
{
return GetRootProxy<TProxy> (_GetRootProxy (name, hostName, server));
}

static TProxy GetRootProxy<TProxy> (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;
}
}
}
1 change: 1 addition & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
12 changes: 6 additions & 6 deletions src/foundation-desktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down

0 comments on commit b481766

Please sign in to comment.