When working with .NET iOS targets (ie. net6.0-ios, net6.0-maccatalyst, Xamarin, MAUI, etc.), there are methods on NSNumber for converting to a specific .NET type. For example, .BooleanValue, .Int32Value, .DoubleValue, etc.
But what should one do if you don't know the type in advance, and just want a .NET object (boxing the actual bool, int, double, or whatever)?
Here's an extension method that I came up with. It uses the
ObjCTypeencoded values, and handlesboolanddecimalvalues properly.It's not perfect though. For example,
NSNumber.FromByte(255).ToObject()gives back anobjectcontaining anInt16, so it would appear that while"c"comes through forsbytevalues, thatbytevalue give"s"rather than"C". Not sure why.