Skip to content

Instantly share code, notes, and snippets.

@brendanzagaeski
Last active April 18, 2020 10:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brendanzagaeski/9337026 to your computer and use it in GitHub Desktop.
Save brendanzagaeski/9337026 to your computer and use it in GitHub Desktop.
Prevent the Xamarin.Android bindings generator from converting a get or set method to a property
# Prevent the Xamarin.Android bindings generator from converting a `get` or `set` method to a property
Xamarin.Android Java bindings projects will by default automatically bind any Java method that starts with "get" to be the getter of a C# property. To prevent this in specific cases, you can set the `propertyName` attribute of the method to the empty string. The same is true for methods that start with "set", except that they will only be converted to property setters if there is already a corresponding property getter. This requirement prevents the creation of set-only properties (see also http://msdn.microsoft.com/en-us/library/ms229006.aspx).
So for example, you would add something like the following to the Metadata.xml file:
```
<attr path="/api/package[@name='com.example.testandroidlib']/class[@name='MyClass']/method[@name='getNumberTen']" name="propertyName"></attr>
```
The original Java method in this case might look like:
```
public int getNumberTen()
{
return 10;
}
```
By default, the bindings generator would bind `getNumberTen()` to be the getter on a `NumberTen` property:
```
public virtual int NumberTen {
[Register ("getNumberTen", "()I", "GetGetNumberTenHandler")]
get {
if (id_getNumberTen == IntPtr.Zero)
id_getNumberTen = JNIEnv.GetMethodID (class_ref, "getNumberTen", "()I");
if (GetType () == ThresholdType)
return JNIEnv.CallIntMethod (Handle, id_getNumberTen);
else
return JNIEnv.CallNonvirtualIntMethod (Handle, ThresholdClass, id_getNumberTen);
}
}
```
But when the "propertyName" is set to the empty string, the bindings generator will instead generate a method named `GetNumberTen()`.
@aka-STInG
Copy link

Nice! Saved my day!

@sdebruyn
Copy link

How can I do this the other way around? I failed to detect the get/set methods. How can I merge them into a property?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment