nrudnyk
nrudnyk

Reputation: 954

Xamarin.Android binding generations for Finalizer

Is there a proper way to work with finalizers to generate proper xamarin.android bindings?

I have original aar lib which is compiled with API level 24. This lib is added to the xamarin.android binding library project. Xamarin targets JDK8 in preferences jdk1.8.0_91.

The original code has protected void finalize() method, so the generated code looks like this: protected override unsafe void Finalize ()

which cause the following error: "Do not override object.Finalize. Instead, provide a destructor."

Are there any suggestions how to omit this, except cut that finalize method from generation on the xamarin side with the <remove-node> tag in Metadata.xml?

Upvotes: 1

Views: 276

Answers (2)

gianlucaparadise
gianlucaparadise

Reputation: 1763

If you add the remove-node, you will remove the finalizer and the code under finalize method will never be called. If you want to re-use the finalize implementation in the destructor, you can add the following to your Metadata.xml file:

<!--  
    ClassName class implemented a Java Finalizer. I have to implement a C# Destructor and call the finalize method.
    The C# Destructor is in the partial class under the Addition folder.
-->
<!-- I change the name of the finalizer method to avoid the conflict with java finalizer -->
<attr path="/api/package[@name='your.package.name']/class[@name='ClassName']/method[@name='finalize' and count(parameter)=0]" name="managedName">InternalFinalize</attr>
<!-- I replace the protected override modifier with the internal modifier -->
<attr path="/api/package[@name='your.package.name']/class[@name='ClassName']/method[@name='finalize' and count(parameter)=0]" name="visibility">internal</attr>

N.B. the visibility modifier is needed to remove the override modifier.

Then you should add the following class under the folder Additions:

namespace Your.Package.Name
{
    public partial class ClassName
    {
        ~ClassName()
        {
            this.InternalFinalize();
        }
    }
}

Upvotes: 1

Jon Douglas
Jon Douglas

Reputation: 13176

You should simply remove the finalize method via the following:

<remove-node path="/api/package[@name='your.package.name']/class[@name='Proper.ClassName']/method[@name='finalize' and count(parameter)=0]" />

Upvotes: 0

Related Questions