Last active
April 10, 2019 17:38
-
-
Save felipeslongo/444d4105c372e1265ad5cf1af1227091 to your computer and use it in GitHub Desktop.
Extension method Cast for <see cref="Java.Lang.Object"/>.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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