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

hamid Darab 21 Reputation points
2022-05-24T05:40:11.75+00:00

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;}
}

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,291 Reputation points Microsoft Vendor
    2022-05-25T09:37:51.887+00:00

    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.

    0 comments No comments