Bug 58844 - [generator] Methods with out parameters can't use interface protocols
Summary: [generator] Methods with out parameters can't use interface protocols
Alias: None
Product: iOS
Classification: Xamarin
Component: Tools (show other bugs)
Version: XI 10.99 (xcode9)
Hardware: PC Mac OS
: Normal enhancement
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2017-08-17 11:53 UTC by Matthew Leibowitz
Modified: 2017-08-18 19:48 UTC (History)
2 users (show)

See Also:
Is this bug a regression?: No
Last known good build:


Description Matthew Leibowitz 2017-08-17 11:53:54 UTC
This is the Objective-C:

    @protocol TIPImageCodec <NSObject>

    @interface TIPImageCodecCatalogue : NSObject
        - (void)removeCodecForImageType:(NSString *)imageType removedCodec:(out id<TIPImageCodec> *)codec;

The equivalent C#:

    interface ITIPImageCodec : INativeObject { }
    [Protocol, Model]
    [BaseType(typeof(NSObject), Name = "TIPImageCodec")]
    interface TIPImageCodec : INativeObject
    [BaseType(typeof(NSObject), Name = "TIPImageCodecCatalogue")]
    interface TIPImageCodecCatalogue
        void RemoveCodec(string imageType, [NullAllowed] out ITIPImageCodec codec);

However, I get a compiler error with the generated `RemoveCodec`:

    The type 'TwitterImagePipeline.ITIPImageCodec' cannot be used as type parameter 'T' in the 
    generic type or method 'Runtime.GetNSObject<T>(IntPtr)'. There is no implicit reference 
    conversion from 'TwitterImagePipeline.ITIPImageCodec' to 'Foundation.NSObject'.

It works with the `TIPImageCodec` type.
Comment 1 Sebastien Pouliot 2017-08-18 02:02:06 UTC
That sounds incorrect

> @protocol TIPImageCodec <NSObject>

should *not* be bound like

    interface ITIPImageCodec : INativeObject { }

    [Protocol, Model]
    [BaseType(typeof(NSObject), Name = "TIPImageCodec")]
    interface TIPImageCodec : INativeObject

but as

    interface ITIPImageCodec { }

    interface TIPImageCodec


- the `[Model]` should not be used unless you're sure it's a good match for a base type. It often is not (and confuse people / create bad API that enforce a base type when the interface is what's really needed). However that should work too (it's an API design issue);

- bindings should not implement `INativeObject`, that's for non-ObjC type (and manual bound p/invoke). When you deal with a protocol then you know it's ObjC (NSObject-based). That code should generate/compile fine;
Comment 2 Matthew Leibowitz 2017-08-18 14:49:50 UTC
That was a combination of old code and pseudocode. But that is not the issue here. The out parameter is.

I know it works with an NSObject-based type, but `TIPImageCodec` is something like a delegate type. The method that I am trying to bind is:

  - (void)removeCodecForImageType:(NSString *)imageType 
                     removedCodec:(out id<TIPImageCodec> *)codec;

As you can see, it takes a `id<TIPImageCodec>` which (I believe) should be able to accept a managed type:

  class MyCodec : NSObject, ITIPImageCodec { }

And I should be able to do this:

  var catalogue = TIPImageCodecCatalogue.SharedInstance;

  // add ITIPImageCodec
  var myCodec = new MyCodec();
  catalogue.SetCodec(myCodec, "com.microsoft.bmp");

  // remove ITIPImageCodec
  ITIPImageCodec codec;
  catalogue.RemoveCodec("com.microsoft.bmp", out codec);

To get this to work, I did this now:

  // API definition
  void RemoveCodec(string imageType, [NullAllowed] out IntPtr codec);

  // extras
  public void RemoveCodec(string imageType, out ITIPImageCodec codec)
    IntPtr codecPtr;
    RemoveCodec(imageType, out codecPtr);
    codec = Runtime.GetINativeObject<ITIPImageCodec>(codecPtr, false);
Comment 3 Sebastien Pouliot 2017-08-18 19:48:33 UTC
Yes, make sense (without the `INativeObject`) and it seems this is not a case we ever hit with Apple's API (grep returned nothing and trying it confirm the generated code won't compile).

Your workaround is fine, that's what the generator should be generating.

Note You need to log in before you can comment on or make changes to this bug.