Last active
March 15, 2023 19:30
-
-
Save Cheesebaron/9876783 to your computer and use it in GitHub Desktop.
C# to Java object wrapper
This file contains 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 System; | |
public class JavaHolder : Java.Lang.Object | |
{ | |
public readonly object Instance; | |
public JavaHolder(object instance) | |
{ | |
Instance = instance; | |
} | |
} |
This file contains 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 System; | |
public static class ObjectExtensions | |
{ | |
public static TObject ToNetObject<TObject>(this Java.Lang.Object value) | |
{ | |
if (value == null) | |
return default(TObject); | |
if (!(value is JavaHolder)) | |
throw new InvalidOperationException("Unable to convert to .NET object. Only Java.Lang.Object created with .ToJavaObject() can be converted."); | |
TObject returnVal; | |
try { returnVal = (TObject) ((JavaHolder) value).Instance; } | |
finally { value.Dispose(); } | |
return returnVal; | |
} | |
public static Java.Lang.Object ToJavaObject<TObject>(this TObject value) | |
{ | |
if (Equals(value, default(TObject)) && !typeof(TObject).IsValueType) | |
return null; | |
var holder = new JavaHolder(value); | |
return holder; | |
} | |
} |
Hi,thank you for your reply, i was trying to post the java object to firebase but the exception was that it could not find properties to serialize from the object...
…Sent from my iPhone
On 12 Apr 2020, at 19:39, Tomasz Cielecki ***@***.***> wrote:
@Cheesebaron commented on this gist.
@maikoly1 why are you serializing it?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
You should probably not serialize JavaHolder
Hi, I have two questions:
- How do I get
Java.Lang.Object
? Does it come from IKVM? - Does it in IKVM named
java.lang.Object
?
@xiaochao00 no this is for Xamarin.Android.
I don't know if there is an equivalent for IKVM.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@maikoly1 why are you serializing it?