0

I am creating a Binding Library from a AAR lib in order to generate a dll that I would be able to use in a Xamarin.Android project.

I have an issue because of a Java authorized syntaxe which is not authorized in C#

How would you write this Java code in C# ?

   public interface IParent{
   }

   public interface IChild extends IParent{
   }

   public interface IGetter{
       IParent getAttribute();
   }

   public class MyClass implements IGetter{
       public IChild getAttribute() {
           return null;
       }
   }

The automatic binding file generated gives me this king of result, which is not authorized

   public interface IParent
   {
   }

   public interface IChild : IParent
   {
   }


   public interface IGetter
   {
       IParent Attribute { get; }
   }

   public class MyClass : IGetter
   {
       public IChild Attribute { get; }    //Not allowed but is the exact Java equivalent
       //public IParent Attribute { get; }    //Allowed but not the exact Java equivalent
   }

I get the following error :

'MyClass' does not implement interface member 'IGetter.Attribute'. 'MyClass.Attribute' cannot implement 'IGetter.Attribute' because it does not have the matching return type of 'IParent'.

I am thinking about creating a entire class that would do the bridge between IChild and IParent, but it must be another solution more suitable...

Maxime Esprit
  • 705
  • 1
  • 8
  • 29
  • 1
    You should have a look at https://stackoverflow.com/questions/1048884/c-overriding-return-types – fredrik Jun 29 '21 at 10:08

1 Answers1

0

Thank you @fredrik

Here is the solution I found thanks to your comment :

public interface IParent
{
}

public interface IChild : IParent
{
}


public interface IGetter<T> where T : IParent
{
    T Attribute { get; }
}

public class MyClass : IGetter<IChild>
{
    public IChild Attribute { get; }   
}

Using templates was indeed the solution.

Maxime Esprit
  • 705
  • 1
  • 8
  • 29