From 07690c841f2f9845d39686e9c9d86306e29edd29 Mon Sep 17 00:00:00 2001 From: Aaron Oneal Date: Fri, 23 Nov 2012 16:57:34 -0800 Subject: [PATCH] Improve exception message when a type cannot be converted during marshaling Now when a type cannot be converted a more detailed exception message is generated stating the types converted from and to. This makes it easier to debug applications. --- src/ObjCRuntime/NSObjectMarshaler.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/ObjCRuntime/NSObjectMarshaler.cs b/src/ObjCRuntime/NSObjectMarshaler.cs index 2734cc346..056c9b30c 100644 --- a/src/ObjCRuntime/NSObjectMarshaler.cs +++ b/src/ObjCRuntime/NSObjectMarshaler.cs @@ -29,8 +29,19 @@ namespace MonoMac.ObjCRuntime { public class NSObjectMarshaler : ICustomMarshaler where T : NSObject { static NSObjectMarshaler 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) {