Paypal Parse Java.Lang.Object to PaymentConfirmation in Xamarin
Question
I am building a mobile app for android with Xamarin and I want to use paypal for the user to pay us. After the payment I want to sent the confirmation to our server to check that the payment is good and complet and made the modification relating to the purchased.
I used the Android SDK to create a Java Binding Library. I used the tutorial at: https://github.com/paypal/PayPal-Android-SDK/blob/master/docs/single_payment.md
PaymentConfirmation confirm =
data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
I turned it into C# as so:
PaymentConfirmation confirm =
data.GetParcelableExtra(PaymentActivity.ExtraResultConfirmation);
This give me an exception saying that there exist an explicit cast so I add it:
PaymentConfirmation confirm =
(PaymentConfirmation)data.GetParcelableExtra(PaymentActivity.ExtraResultConfirmation)
This gives the following exception: System.InvaliCastException: Cannot cast from source type to destination type.
I have tried all I could think of so I'm looking for help.
A functional partial project may be found here: https://github.com/PhilSim22/PartialProject/tree/master
I am under a nondisclosure agreement so everything not related to the issue have been stripped out. sorry for the inconveniant.
Solution
I found the answer. I had to use a JAVA cast instead of a normal cast. here is my solution:
var confirmObj = data.GetParcelableExtra (PaymentActivity.ExtraResultConfirmation);
PaymentConfirmation confirm = Android.Runtime.Extensions.JavaCast<PaymentConfirmation> (confirmObj);
OTHER TIPS
im you don't find the solution maybe the component parse.com can help you: https://parse.com/tutorials/integrating-with-third-party-services
With any invalid cast exception, the first thing I would try would be to store the result you wish to cast into a temporary variable, and look at its type, either in Debug mode or through logging.
Therefore I would do something like this:
object temp = data.GetParcelableExtra(PaymentActivity.ExtraResultConfirmation);
Console.WriteLine(temp.GetType().FullName);
Console.WriteLine("Can Assign: {0}", typeof(PaymentConfirmation).IsAssignableFrom(temp.GetType()));
This way, you could see if the returned type matches the casted type PaymentConfirmation.
Can you share the full class name of the source and destination classes?