Skip to content
This repository was archived by the owner on Aug 27, 2026. It is now read-only.
This repository was archived by the owner on Aug 27, 2026. It is now read-only.

Compilation error using parameter arrays in EventArgs #817

Closed

Description

@vanka78bg

I have a C# wrapper library for a native Android Java library. The wrapper library compiles just fine with Xamarin.Android, but gives me compile-time errors when trying to migrate to .NET 6.0 Android.

Given a Java interface like this:

public interface SomeListener {
    void onSomeNotification(long p0, long p1, long p2, Object... p3);
}

The following C# code is auto-generated:

public partial interface ISomeListener : IJavaObject, IJavaPeerable {
    void OnSomeNotification(long p0, long p1, long p2, params global::Java.Lang.Object[] p3);
}

public partial class SomeEventArgs : global::System.EventArgs {
    public SomeEventArgs (long p0, long p1, long p2, params global::Java.Lang.Object[] p3)
    {
        this.p0 = p0;
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    long p0;

    public long P0 {
        get { return p0; }
    }

    long p1;

    public long P1 {
        get { return p1; }
    }

    long p2;

    public long P2 {
        get { return p2; }
    }

    params global::Java.Lang.Object[] p3;

    public params global::Java.Lang.Object[] P3 {
        get { return p3; }
    }
}

Take note on the last few lines where the params keyword is used incorrectly.
The above auto-generated code fails to compile with the following build error:
"Invalid token 'params' in class, record, struct, or interface member declaration"

Activity

  1. changed the title [-].NET 6 Android: Java ellipsis construct confuses the generator[/-] [+].NET 6 Android: Java ellipsis construct causes compile-time errors in C#[/+] on Mar 18, 2021
  2. jpobst commented on Mar 18, 2021

    @jpobst
    Contributor

    There are no differences between the code-generation process generator.exe in "legacy" and .NET 6, so it seems odd that you would get different code between the two. These issues also all seem like things that should fail on legacy bindings projects as well.

    My guess is that you have fixed these issues in metadata.xml file(s), and these files are not getting picked up in your .NET 6 project. There hasn't been much testing yet on .NET6 binding projects, so this could be an issue on our side or an issue with the way you have constructed your project.

    Here's some documentation on how they are supposed to work:
    https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/OneDotNet.md#consolidation-of-binding-projects

    So for first steps, let's confirm that your legacy project does indeed rely on metadata.xml files, and then please try to determine if you think you have set them up correctly in your .NET6 project. If everything looks good on your side, let me know, and I can dig deeper on our side.

    The only other thing I can think of is if you were using <AndroidCodegenTarget>XamarinAndroid</AndroidCodegenTarget> previously, which is no longer available in .NET6, but I really don't think that change would cause issues of this type.

  3. vanka78bg commented on Mar 19, 2021

    @vanka78bg
    Author

    OK, thank you for the tip. I'll double-check if I have messed up my metadata.xml when migrating the project. Still, I believe placing the params keyword as a part of the type signature is a bug in the C# code generator. Ideally, we should not workaround such things by hand.

  4. vanka78bg commented on Mar 26, 2021

    @vanka78bg
    Author

    I have double-checked my metadata.xml files and there is nothing in there to suppress the generation of the offending classes. Since nothing has changed, except migrating my Xamarin.Android projects to .NET SDK projects targeting .NET 6.0 android, I have no explanation why this is failing now. Regretfully, I cannot share the source code of the library, because it is owned by my company. What I can do is to create a small repro project, if you cannot reproduce the issue on your side.

  5. jpobst commented on Mar 26, 2021

    @jpobst
    Contributor

    Out of curiousity, what version of Visual Studio are you using to compile the "legacy" version? There was a large generator refactor in 16.9 that may have caused this breakage. The .NET 6 version is the same as the 16.9 version.

    The workaround would be to use metadata to override the parameter array type.

    <attr path="/api/package[@name='my.package']/interface[@name='SomeListener']/method[@name='onSomeNotification']/parameter[@name='p3']" name="managedName">Java.Lang.Object[]<attr>
    
  6. changed the title [-].NET 6 Android: Java ellipsis construct causes compile-time errors in C#[/-] [+]Compilation error using parameter arrays in EventArgs[/+] on Mar 26, 2021
  7. vanka78bg commented on Mar 26, 2021

    @vanka78bg
    Author

    I am using Visual Studio 16.10 preview 1. The official Visual Studio version 16.9 complains that it cannot recognize the net6.0-android target, even though I have installed the necessary workloads and created the EnableWorkloadResolver.sentinel file, as described here: https://github.com/dotnet/net6-mobile-samples.

  8. removed their assignment
    on May 5, 2021
  9. added
    bugComponent does not function as intended
    generatorIssues binding a Java library (generator, class-parse, etc.)
    on May 5, 2021
  10. locked and limited conversation to collaborators on Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugComponent does not function as intendedgeneratorIssues binding a Java library (generator, class-parse, etc.)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions