Xamarin C# alternative toGetParcelableExtra(String name) which has been deprecated

fbs419 46 Reputation points
2022-10-21T21:27:42.167+00:00

I have this code for NFC tags:

protected override void OnNewIntent(Intent intent)	// NFC tag discovered  
{  
	String action = intent.Action;  
        Tag tag = (Tag)intent.GetParcelableExtra(NfcAdapter.ExtraTag);  
        etc.  

The Visual Studio compiler tells me that GetParcelableExtra(string) has been deprecated. Java docs tell me:

This method was deprecated in API level 33.
Use the type-safer getParcelableExtra(java.lang.String, java.lang.Class) starting from Android Build.VERSION_CODES#TIRAMISU.

which is: public T getParcelableExtra (String name, Class<T> clazz)

But there are no samples for C#. And there is really no C# equivalent for Class<T>. There is typeof, and I saw some posts on this, but I couldn't figure out a decent way to use this new method in C#. Is there a sample for this method that will help? Thanks.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-10-24T05:59:38.82+00:00

    Hello,

    And there is really no C# equivalent for Class<T>. There is typeof, and I saw some posts on this, but I couldn't figure out a decent way to use this new method in C#. Is there a sample for this >method that will help?

    Please try to use Java.Lang.Class.FromType(typeof(Tag)) to convert from 'System.Type' to 'Java.Lang.Class' like following code.

       Tag tag = (Tag)intent.GetParcelableExtra(NfcAdapter.ExtraTag,Java.Lang.Class.FromType(typeof(Tag)));  
    

    Best Regards,

    Leon Lu


    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.

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.