Bug 2147 - Type conversion + wrapping support
Summary: Type conversion + wrapping support
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 1.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-11-21 22:51 UTC by Jonathan Pryor
Modified: 2012-04-30 16:22 UTC (History)
1 user (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 FIXED

Description Jonathan Pryor 2011-11-21 22:51:43 UTC
We currently provide Android.Runtime.JavaList, JavaDictionary, etc. types.

The problem is that using these types is "wonky" and loses type information:

> try 
> { 
>     Android.Runtime.JavaList<Dictionary<String, object>> jl = new Android.Runtime.JavaList<Dictionary<String, object>>(); 
>     Dictionary<String, object> v = new Dictionary<String, object>(); 
v.Add("szLongName", "Bla.pdf"); 
>     jl.Add(v); // Exception 
> } 
> catch(Exception ex) 
> { 
> } 

Exception: 

> Cannot cast from source type to destination type. 
> at Android.Runtime.JavaTypeConverter`1+JavaDictionaryConverter`1[System.Collections.Generic.Dictionary`2[System.String,System.Object],System.Collections.Generic.Dictionary`2[System.String,System.Object]].ToNative (System.Collections.Generic.Dictionary`2 val) [0x0001c] in /Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JavaTypeConverter.cs:298
> at Android.Runtime.JavaList`1[System.Collections.Generic.Dictionary`2[System.String,System.Object]].Add (System.Collections.Generic.Dictionary`2 item) [0x00000] in /Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JavaList.cs:250
> at HelloM4A.Activity3.OnCreate (Android.OS.Bundle bundle) [0x00023] in /Users/uh/Projects/HelloM4A/HelloM4A/Activity3.cs:37 


> try 
> { 
>     Android.Runtime.JavaList jl = new Android.Runtime.JavaList(); 
Dictionary<String, object> v = new Dictionary<String, object>(); 
>     v.Add("szLongName", "Bla.pdf"); 
>     jl.Add(v); 
>     Dictionary<String, object> vo = (Dictionary<String, object>)jl[0]; // Exception 
> } 
> catch(Exception ex) 
> { 
> } 

Exception: 

> Cannot cast from source type to destination type. 
> at Android.Runtime.JavaTypeConverter`1+JavaDictionaryConverter`1[System.Collections.Generic.Dictionary`2[System.String,System.Object],System.Collections.Generic.Dictionary`2[System.String,System.Object]].ToNative (System.Collections.Generic.Dictionary`2 val) [0x0001c] in /Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JavaTypeConverter.cs:298
> at Android.Runtime.JavaList`1[System.Collections.Generic.Dictionary`2[System.String,System.Object]].Add (System.Collections.Generic.Dictionary`2 item) [0x00000] in /Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JavaList.cs:250
> at HelloM4A.Activity3.OnCreate (Android.OS.Bundle bundle) [0x00023] in /Users/uh/Projects/HelloM4A/HelloM4A/Activity3.cs:37
Comment 1 Jonathan Pryor 2011-11-21 22:54:56 UTC
The cause for this is that JavaTypeConverter & co. isn't fully recursive. For example, to support:

> var list = new JavaList ();
> list.Add (new Dictionary<string, object>());

this is ~equivalent to:

> java.lang.ArrayList list = new java.lang.ArrayList();
> mono.android.JavaObject value = new mono.android.JavaObject();
> list.add(value);

which sounds fine, but when we retrieve the value from the JavaList, we don't "unwrap" the JavaObject to re-obtain the original Dictionary. Oops.

What we need to instead do is a System.Convert-like API which is (1) public, and (2) recursive, wrapping and unwrapping with full fidelity and equivalence.
Comment 2 Jonathan Pryor 2012-04-30 16:21:58 UTC
Fixed in 8f38086d. We're not yet providing a public System.Convert-like API, unless we need to.

We should no longer have the JavaObject type "leak" into apps; apps should always get out what they pass in.