title: Xamarin.iOS C# interoperability with objective-c: callbacks from objective-c to non-static C# methods
body: <p>I'm an objective-c novice, so please forgive what may be incorrect terminology.</p>
  
  <p>In Xamarin.iOS, I am successfully employing a C# class with multiple static methods to implement callback functionality from objective-C to C#.  I'd like to avoid the creation of a binding library (don't want to use Sharpie and related tools / steps).  I'm looking for a way for objective-c to callback to non-static methods in C#.   Is it possible for objective-c to callback to non-static Xamarin.iOS C# methods? My objective-c to C# call-back technique is as follows and is further described below:</p>
  
  <ul>
  <li>Create a class in C# that contains my static call-back methods.  This class inherits from NSObject.</li>
  <li>Create a @protocol in objective-c that matches the "call-back" class defined in C#.  </li>
  <li>Instantiate the "call-back" class in C# and get its NSObject.Class.Handle</li>
  <li>Pass the NSObject.Class.Handle for the "call-back" class instance from C# to objective-c, </li>
  <li>In objective-c, cast the handle to an id (for the previously defined @protocol) and use this id as a delegate to the C# class instance (which now appears as an @interface instance in objective-c). </li>
  </ul>
  
  <p>Using this technique, I am able to treat my C# "call-back" class instance as an @interface instance in objective-c.  In objective-c, I can call methods in my C# class instance by treating them as @interface instance methods.</p>
  
  <p><strong>In C#</strong></p>
  
  <ul>
  <li>Define a class that inherits from NSObject (NSObject includes Class.Handle definition)</li>
  <li>[Register] the class and [Export] the static callbacks within the class</li>
  <li>Use 'NSObject.Class.Handle' to get IntPtr class handle</li>
  <li>Marshal the class instance handle (IntPtr) to objective-c as 'UnmanagedType.IUnknown'</li>
  </ul>
  
  <p><strong>In objective-c</strong></p>
  
  <ul>
  <li>define a '@protocol' that corresponds to the C# class. The @protocol has instance methods that correspond to each static callback method in the C# class</li>
  <li>cast the IntPtr (from C#) as an 'id' for the '@protocol'</li>
  <li>use the id as a delegate to the C# class instance (which behaves as an objective-c @interface instance)</li>
  </ul>
  
  <p>Using this technique, I can use the C# class handle as a protocol id in objective-c and reference the C# class as an @interface in objective-c.</p>
  
  <p>Can this same (or similar) method (passing a class handle from C# to objective-c and using this class handle as the delegate) be employed to call non-static C# methods from objective-c?</p>
  
  <pre><code>    // C# "call-back" class
      [Register("AppDelegate")]
      public class AppDelegate : NSObject
      {
          [Export("OnLoginSucceeded:")]
          public static void OnLoginSucceeded(string sessionToken)
          {
              // Do stuff
          }
  
          [Export("OnRequestFailed")]
          public static void OnRequestFailed()
          {
              // Do stuff
          }
  
          [Export("OnConnectionFailed")]
          public static void OnConnectionFailed()
          {
              // Do stuff
          }
  
          public IntPtr GetInstancePointer()
          {
              IntPtr self = Class.Handle;
              return self;
          }
      }
  
  
      // objective-c protocol for C# "call-back" class
      #import &lt;Foundation/Foundation.h&gt;
  
     @protocol AppDelegate &lt;NSObject&gt;
      - (void)OnLoginSucceeded : (NSString*)sessionToken;
      - (void)OnRequestFailed;
      - (void)OnConnectionFailed;
  
      @end
  
  
      // objective-c calls to C# callback
      [self.appDelegate OnLoginSucceeded : response.sessionToken];
      [self.appDelegate OnRequestFailed];
      [self.appDelegate OnConnectionFailed];
  </code></pre>