Bug 16604 - WindowManagerImpl fails to cast to IWindowManager
Summary: WindowManagerImpl fails to cast to IWindowManager
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
Depends on:
Reported: 2013-12-04 14:12 UTC by Sami M. Kallio
Modified: 2013-12-04 15:43 UTC (History)
1 user (show)

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


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:


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

Note You need to log in before you can comment on or make changes to this bug.