Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Improve exception message when a type cannot be converted during marshaling #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/ObjCRuntime/NSObjectMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ namespace MonoMac.ObjCRuntime {
public class NSObjectMarshaler<T> : ICustomMarshaler where T : NSObject {
static NSObjectMarshaler<T> marshaler;

public object MarshalNativeToManaged (IntPtr handle) {
return (T) Runtime.GetNSObject (handle);
public object MarshalNativeToManaged (IntPtr handle)
{
if (handle == IntPtr.Zero)
return null;

var obj = Runtime.GetNSObject (handle);
if (!(obj is T)) {
throw new MarshalDirectiveException (
string.Format ("NSObjectMarshaler<{0}>: Could not cast from type {1}", typeof(T), obj.GetType ()));
}
else {
return (T) obj;
}
}

public IntPtr MarshalManagedToNative (object obj) {
Expand Down