Bug 41376 - Comparing UITextSelectionView to UIView returns true using GetType() and typeof
Summary: Comparing UITextSelectionView to UIView returns true using GetType() and typeof
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 9.8 (tvOS / C7)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-05-30 09:01 UTC by a4c8
Modified: 2016-06-07 11:23 UTC (History)
5 users (show)

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:

Description a4c8 2016-05-30 09:01:28 UTC
In short: Comparing a subtype of UITextView (e.g. UITextSelectionView) with UIView using GetType() and typeof returns true, although it should return false, because the types aren't the same.

One seems to need to use ((object)view).Class.Name to retrieve the "real name" which is shown during debugging and compare it to get the right result. Is this intended? To me GetType() works unexpectedly.

More details at the stackoverflow-thread: http://stackoverflow.com/questions/37464813/weird-type-check-results-comparing-uitextselectionview-to-uiview
Comment 1 a4c8 2016-06-04 08:21:50 UTC
The property "Class" of an object is only available at runtime - and only if I explicitly cast anything - even an object - to object.

So no way to check for types - what is going on there?
Comment 2 Sebastien Pouliot 2016-06-04 15:12:34 UTC
Like I explained on stackoverflow this is normal. Neither Xamarin or Apple expose the _private_ types.

You get a `UIView` because:

a. this is the type that `Subviews` returns; and

b. it's the closest, *public*, type that match the native instance type being returned;

Still you can access the private types (e.g. using `Class.Name`) but that will only make your code break the next time Apple changes iOS internals. This is just like peeking into the .NET BCL using reflection: you might find interesting things but no one will guarantee it will keep working in the future.

IOW those types are private for a good reason.
Comment 3 a4c8 2016-06-06 18:50:43 UTC
I disagree. GetType() and typeof checks for the _exact_ type and no derived subtype. If it is possible to show the type during debugging it should also be possible to return it using GetType.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2016-06-07 11:23:31 UTC
In order to return a type using GetType it must be known at compile time (this is because Xamarin.iOS can't emit code at runtime because iOS does not allow it).

It's not possible to know about private types at compile time (they can even change between iOS versions, since they're by definition private).

Thus it's not possible to return a private iOS type using GetType, instead you'll get the closest public type.

If you want to filter objects to those whose underlying Objective-C type is exactly UIView, you can try something like this:

    if (element.Class.Handle != ObjCRuntime.Class.GetHandle (typeof (UIView)) {
        // not UIView