0

I'm developing an Android application using Xamarin. For this app, I'll need to use two .jar libraries. These libraries do exactly the same thing, the difference is only that one should be used in a production environmentand the other is for development usage.

One of the requirements for that application, is that it must have a configuration that the user can set, to specify the environment (Production or Development).

I need to interact with a class of those libraries (let's call it Foo). If I needed to create that class using C#, I would first create an interface named IFoo and then create the two classes implementing that interface, then I would use a factory to create the correct instance based on the preference that the user set. But the problem is that on the Java libraries, both of those classes inherith directly from Object, so my calling code will not be able to use an abstraction as I wanted.

I was wondering, if it's possible to use the Metadata transform to modify the generated code to make the classes implement an interface. I was able to make it work by modifing the generated code, but every time I recompile the project, I miss my modifications.

1
  • You could probably use <add-node> appropriately to specify the specifics of what you want the class/interface to do: gist.github.com/JonDouglas/dda6d8ace7d071b0e8cb#adding-types You should look through your generated api.xml for the definition of your Foo interface for a scaffolding to fix this. Commented Jun 21, 2016 at 21:33

1 Answer 1

2

Your generated classes are partial. This means that you can extend them easily.

generated

[global::Android.Runtime.Register ("com/bar/baz/Foo", DoNotGenerateAcw=true)]
public partial class Foo : global::Java.Lang.Object {

}

addition

Add this (Foo.cs) to the folder Additions

public partial class Foo : IFoo 
{
    public void WhatEverFooDoes() 
    {
        // implement me
    }
}

IFoo can define methods that are implemented by the generated code as well. So you just have to extract the interface from one of you classes, if the signature is exactly the same.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sven! I didn't notice those generated classes were partial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.