question

hamidDarab-9430 avatar image
0 Votes"
hamidDarab-9430 asked YonglunLiu-MSFT commented

How Cast(Convert) Java.Lang.Object derived to C# object

Hi. I'm going to use Microsoft SignalR with Xamarin.Android. so I need to setup 3 projects;
-Web Application(as Server)
-Xamarin.Android( as Client)
-Shared Project (containing Models)
suppose we have a simple POCO C# class which is shared between projects(Web Application , Blazor, Xamarin.Android)
in many scenarios in Xamarin.Android (like overridden functions or events) the passed arguments to the function is a Java.lang.object. I want to know how I can cast Java.lang.object parameter to my POCO shared object(object derived).
If I inherit the POCO shared class from Object :
1-I can not use it in many methods in Xamarin.Android (like RecyclerView.Adapter ,...)
if the POCO shared class inherits from Java.lang.object , then :
1-I can not use it as shared between different dotnet projects
2-the dotnet json serializer doesn't work.
Thanks.
Example:
public class PocoClass
{
public int Id {get;set;}
public string UserName {get;set;}
public string LastName {get;set;}
}







dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·

1 Answer

YonglunLiu-MSFT avatar image
0 Votes"
YonglunLiu-MSFT answered

Hello,

You could refer to the following file, it contains code for casting Java.Lang.Object to C# object.

205379-castcode.txt

Usage example:

POCO poco = ObjectTypeHelper.Cast<POCO>(obj);


Best Regards,

Alec Liu.



If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



castcode.txt (280 B)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.