Skip to content

Instantly share code, notes, and snippets.

@aka-STInG
Forked from brendanzagaeski/gist:9337026
Created January 22, 2016 09:38
Show Gist options
  • Save aka-STInG/298cf9b043cdf5d06fb5 to your computer and use it in GitHub Desktop.
Save aka-STInG/298cf9b043cdf5d06fb5 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()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment