private static string DoGetMessage(NSObject instance) { string text; if (instance.isMemberOfClass(new Class("NSException"))) { text = Marshal.PtrToStringAuto((IntPtr)instance.Call("name").Call("UTF8String")); text += ". "; text += Marshal.PtrToStringAuto((IntPtr)instance.Call("reason").Call("UTF8String")); } else { text = Marshal.PtrToStringAuto((IntPtr)instance.Call("description").Call("UTF8String")); } return(text); }
/// <summary>Converts an <c>NSException</c> into a managed exception and throws it.</summary> /// <param name = "instance">The <a href ="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsexception_Class/Reference/Reference.html">NSException</a> /// pointer.</param> /// <remarks>If the <c>NSException</c> wraps a managed exception then /// <c>TargetInvocationException</c> is thrown instead with the inner exception /// set to the original exception.</remarks> public static void Raise(IntPtr instance) { NSObject obj = new NSObject(instance); if (obj.isMemberOfClass(new Class("NSException"))) { // See if the userInfo contains a .NET exception. NSObject userInfo = (NSObject)obj.Call("userInfo"); if (userInfo != null && (IntPtr)userInfo != IntPtr.Zero) { IntPtr keyBuffer = Marshal.StringToHGlobalAuto(".NET exception"); NSObject key = (NSObject) new Class("NSString").Call("alloc").Call("initWithUTF8String:", keyBuffer); key.autorelease(); Marshal.FreeHGlobal(keyBuffer); NSObject data = (NSObject)userInfo.Call("objectForKey:", key); if (data != null && !data.IsNil()) { // If it does then get the serialized exception bytes, IntPtr ptr = (IntPtr)data.Call("bytes"); uint bytes = (uint)data.Call("length"); // copy them into a managed buffer, byte[] buffer = new byte[bytes]; Marshal.Copy(ptr, buffer, 0, (int)bytes); // and raise the original exception. using (MemoryStream stream = new MemoryStream(buffer)) { BinaryFormatter formatter = new BinaryFormatter(); Exception e = (Exception)formatter.Deserialize(stream); throw new TargetInvocationException("Exception has been thrown by the (managed) target of an Objective-C method call.", e); // yes TargetInvocationException sucks, but it preserves the original stack crawl... } } } } throw new CocoaException(obj); }
/// <summary>Converts an <c>NSException</c> into a managed exception and throws it.</summary> /// <param name = "instance">The <a href ="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsexception_Class/Reference/Reference.html">NSException</a> /// pointer.</param> /// <remarks>If the <c>NSException</c> wraps a managed exception then /// <c>TargetInvocationException</c> is thrown instead with the inner exception /// set to the original exception.</remarks> public static void Raise(IntPtr instance) { NSObject obj = new NSObject(instance); if (obj.isMemberOfClass(new Class("NSException"))) { // See if the userInfo contains a .NET exception. NSObject userInfo = (NSObject) obj.Call("userInfo"); if (userInfo != null && (IntPtr) userInfo != IntPtr.Zero) { IntPtr keyBuffer = Marshal.StringToHGlobalAuto(".NET exception"); NSObject key = (NSObject) new Class("NSString").Call("alloc").Call("initWithUTF8String:", keyBuffer); key.autorelease(); Marshal.FreeHGlobal(keyBuffer); NSObject data = (NSObject) userInfo.Call("objectForKey:", key); if (data != null && !data.IsNil()) { // If it does then get the serialized exception bytes, IntPtr ptr = (IntPtr) data.Call("bytes"); uint bytes = (uint) data.Call("length"); // copy them into a managed buffer, byte[] buffer = new byte[bytes]; Marshal.Copy(ptr, buffer, 0, (int) bytes); // and raise the original exception. using (MemoryStream stream = new MemoryStream(buffer)) { BinaryFormatter formatter = new BinaryFormatter(); Exception e = (Exception) formatter.Deserialize(stream); throw new TargetInvocationException("Exception has been thrown by the (managed) target of an Objective-C method call.", e); // yes TargetInvocationException sucks, but it preserves the original stack crawl... } } } } throw new CocoaException(obj); }
private static string DoGetMessage(NSObject instance) { string text; if (instance.isMemberOfClass(new Class("NSException"))) { text = Marshal.PtrToStringAuto((IntPtr) instance.Call("name").Call("UTF8String")); text += ". "; text += Marshal.PtrToStringAuto((IntPtr) instance.Call("reason").Call("UTF8String")); } else text = Marshal.PtrToStringAuto((IntPtr) instance.Call("description").Call("UTF8String")); return text; }
public bool validateUserInterfaceItem(NSObject sender) { bool enabled = false; Selector sel = (Selector) sender.Call("action"); if (sel.Name == "dirHandler:") { int tag = (int) sender.Call("tag"); var handler = m_boss.Get<IMenuHandler>(); MenuState state = handler.GetState(tag); enabled = (state & MenuState.Enabled) == MenuState.Enabled; if (sender.respondsToSelector("setState:")) sender.Call("setState:", (state & MenuState.Checked) == MenuState.Checked ? 1 : 0); if (enabled && tag == 50 && sender.isMemberOfClass(NSMenuItem.Class)) { Unused.Value = sender.Call("setTitle:", NSString.Create("Build " + m_name)); } else if (enabled && tag == 1000 && sender.isMemberOfClass(NSMenuItem.Class)) { Unused.Value = sender.Call("setTitle:", NSString.Create(m_name + " Preferences")); } } else if (sel.Name == "targetChanged:") { enabled = m_builder != null && m_builder.CanBuild; } else if (sel.Name == "renameItem:") { NSIndexSet selections = m_table.selectedRowIndexes(); enabled = selections.count() == 1; } else if (sel.Name == "duplicate:") { NSIndexSet selections = m_table.selectedRowIndexes(); enabled = selections.count() > 0 && m_table.editedRow() < 0; // cocoa crashes if we do a duplicate while editing... } else if (respondsToSelector(sel)) { enabled = true; } else if (SuperCall(NSWindowController.Class, "respondsToSelector:", new Selector("validateUserInterfaceItem:")).To<bool>()) { enabled = SuperCall(NSWindowController.Class, "validateUserInterfaceItem:", sender).To<bool>(); } return enabled; }