Skip to content

Instantly share code, notes, and snippets.

@felipeslongo
Last active April 10, 2019 17:38
Show Gist options
  • Save felipeslongo/444d4105c372e1265ad5cf1af1227091 to your computer and use it in GitHub Desktop.
Save felipeslongo/444d4105c372e1265ad5cf1af1227091 to your computer and use it in GitHub Desktop.
Extension method Cast for <see cref="Java.Lang.Object"/>.
using Java.Lang;
namespace App.Droid.Extensions
{
/// <summary>
/// Extension method Cast for <see cref="Java.Lang.Object"/>.
/// </summary>
/// <seealso cref="https://stackoverflow.com/questions/6594250/type-cast-from-java-lang-object-to-native-clr-type-in-monodroid"/>
public static class JavaLangObject_Cast
{
/// <summary>
/// Cast a Java Object to the CLR C# object instance.
/// Extract the wrapped object inside the java one.
/// </summary>
/// <param name="this"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T Cast<T>(this Object @this) where T : class
{
var propertyInfo = @this.GetType().GetProperty("Instance");
return propertyInfo == null ? null : propertyInfo.GetValue(@this, null) as T;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment