Bug 21253 - Cannot retrieve .Net object from BaseAdapter unless it implements IJavaObject
Summary: Cannot retrieve .Net object from BaseAdapter unless it implements IJavaObject
Status: RESOLVED NORESPONSE
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.12.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2014-07-10 19:11 UTC by David Schwegler
Modified: 2017-06-28 16:43 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NORESPONSE

Description David Schwegler 2014-07-10 19:11:44 UTC
Background:

BaseAdapter<T>'s GetItem is implemented as taking the .net object and wrapping it in a Java object (which is fine):
    public override Java.Lang.Object GetItem(int position)
    {
      return JavaObjectExtensions.JavaCast<Java.Lang.Object>(JavaConvert.ToJavaObject<T>(this[position]));
    }

However, the only exposed way to un-wrap this object is via JavaCast. However, that requires that the return type be an IJavaObject: 
    public static TResult JavaCast<TResult>(this IJavaObject instance) where TResult : class, IJavaObject


Problem:

I have an object that actually is a .net object, and does not implement IJavaObject. However, it was originally obtained via GetItem and thus wrapped into an Java Object. I have no access to the original adapter at this point of the code.

Therefore, I have no way to convert this Java Object back to my .net object, short of doing manual reflection. (JavaConvert.FromJavaObject<> would work, but it is marked as internal.)
Comment 1 Sadik Ali 2014-11-03 04:29:58 UTC
I have checked the definition of BaseAdapter<T> and Javacast.

Screencast regarding same:
http://screencast.com/t/Q1YpWtGMiz

I’ll need confirmation from the developer if this is a bug. 

Leaving as NEW for now.
Comment 2 Jonathan Pryor 2014-11-03 12:24:03 UTC
To some extent, the problem is that Java.Lang.Object exists as parameter or return types at all; see Bug #6085.  That won't be fixed anytime soon.

In the meantime, how are you getting the internal wrapper? Ideally, you shouldn't know that it's there...which clearly isn't happening. Adding some public "unwrap" method also isn't ideal, as then you'd need to know it exists so that you can manually unwrap instances, which just seems like a different kind of pain.

I'd like to better understand your usage scenario, so I can see if there's a better way to support it.
Comment 3 Jon Douglas [MSFT] 2017-06-28 16:43:06 UTC
I am closing this bug due to a lack of response requested by https://bugzilla.xamarin.com/show_bug.cgi?id=21253#c2 

Please feel free to reopen this bug once you can add sufficient information.