Forum Xamarin Xamarin.iOS

iOS Native Binding: How to hanlde a derived return type

bnofferbnoffer DEMember ✭✭

Hi everyone,

I am facing a problem while creating a binding for a iOS native library.

Lets say I have a base type A and two types that inherit from A, so Aa and Ab. The Header information for the library contains a Class B which has a Method getA with the following signature:

@required -(id<A> _Nonnull)getA __attribute__((warn_unused_result));

But the logic expects Aa or Ab to be returned.

The binding code generated in ApiDefinition.cs looks something like this:

   interface IA { }

   [Protocol, Model]
   interface A
      // A declaration

   interface IAa { }

   [Protocol, Model]
   interface Aa : A
      // Aa declaration

   interface IAb { }

   [Protocol, Model]
   interface Ab : A
      // Ab declaration

The method as part of B is declared like this:

   // @required -(id<A> _Nonnull)getA __attribute__((warn_unused_result));
   [Export ("getA")]
   IA A { get; }

When I now call var result = B.A; from C# I get a AWrapper instance that is auto-generated by the binding project even if the Object provided is of the Type Aa or Ab. Therefore all additional information in Aa or Ab that are not part of A get lost.

Is there any way to fix this so I get to the additional information of Aa or Ab?

Thank you in advance and best regards,


  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 2018

    No better idea, i use this way.

     var isAa = result is IAa;
     if (isAa) {result = result as IAa; }
     else { result = result as IAb ; }
Sign In or Register to comment.