Can I override generated Java method return type? (C# code breaks Java covariance)

AntonMorozovMCAntonMorozovMC USMember
edited August 2015 in Xamarin.Android


I have implemented a custom layout, inherited from FrameLayout. Base class, ViewGroup, provides couple of virtual methods that return ViewGroup.LayoutParams, and descendant classes are supposed to override them, returning subclasses, like FrameLayout.LayoutParams.

Here comes the problem - C# version of FrameLayout (and hence my subclass) has those methods return ViewGroup.LayoutParams, but generated Java code causes an error, because Java version of FrameLayout returns FrameLayout.LayoutParams, and can not be overriden by a method with a more generic return value.

Graphic representation:
Class Java C# ViewGroup VG.LP Foo() FrameLayout FL.LP Foo() --> VG.LP Foo() MyLayout cannot override! <-- VG.LP Foo()

Actually, Xamarin does not generate an overridden method in FrameLayout at all, so it stays with base return type.

This leads me to my question: is there a way to control / override the return type of generated Android-callable Java method?

Of course, I can just roll up my sleeves and rewrite my class to inherit from ViewGroup directly (there ain't much code in FrameLayout, after all), but is there a language-level solution? It's not like I can modify how Xamarin binds to Android code, isn't it?

Sign In or Register to comment.