Bug 16604 - WindowManagerImpl fails to cast to IWindowManager
Summary: WindowManagerImpl fails to cast to IWindowManager
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries (show other bugs)
Version: 4.10.1
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-12-04 14:12 UTC by Sami M. Kallio
Modified: 2013-12-04 15:43 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 NOT_ON_ROADMAP

Description Sami M. Kallio 2013-12-04 14:12:35 UTC
The following code demonstrates the bug:

			var wm = Application.Context.GetSystemService(Context.WindowService);
			// wm = WindowManagerImpl
			var v = wm as IWindowManager;
			// v = null

Xamarin.Android 4.2.1 Business Edition, stable release.
Comment 1 Jonathan Pryor 2013-12-04 15:43:12 UTC
This is by design. When a Java object enters managed code and a wrapper is constructed for it, we only check the type and all base types; we do not enumerate and check every interface that the type implements, on the assumption that a base class would be more handy than the first interface the type happens to implement (which could vary over time, anyway).

The correct thing to do here is to use the JavaCast<T>() extension method:

http://androidapi.xamarin.com/?link=M%3aAndroid.Runtime.Extensions.JavaCast%3CTResult%3E

    var wm = Application.Context.GetSystemService(Context.WindowService);
    var v   = wm.JavaCast<IWindowManager>();