Bug 15903 - Cannot retrieve PropertyInfo for HighlightedImage property of UIImageView when running on device
Summary: Cannot retrieve PropertyInfo for HighlightedImage property of UIImageView whe...
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 7.0.2.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-11-04 09:56 UTC by Jarrod Koch
Modified: 2013-11-04 11:29 UTC (History)
2 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 Jarrod Koch 2013-11-04 09:56:11 UTC
Using the GetProperty() method of Type, a PropertyInfo for the HighlightedImage property of the UIImageView class can be retrieved when running on an iOS 7.0.3 simulator on OS X, but when running the same code on an iPad running iOS 7.0.3, null is returned.

HighlightedImage is a public instance property of UIImageView so there should not be any problems with retrieving information about it through reflection.

The following line of code, run on the simulator and a device, should make the issue apparent:

System.Reflection.PropertyInfo pi = typeof(MonoTouch.UIKit.UIImageView).GetProperty("HighlightedImage");

pi will be null when run on a device, but will have the expected value when run on a simulator.
Comment 1 Sebastien Pouliot 2013-11-04 10:49:24 UTC
That's normal. The (managed) linker [1] is enabled, by default, on device builds and will remove unused code - while it's not enabled on simulator builds.

The linker use static analysis to detect what's must be kept inside the application. Static analysis cannot detect reflection usage (it's dynamic) so you'll need to give some hints to the linker.

One way to hint this is to add a bit of extra code. If your application uses the property (make sure to use both getter and setters if you need both) then it will be kept and available for reflection at runtime.

E.g. a static ctor (which the linker won't remove) could do something like:

static MyType ()
   UIImageView v = null;
   if (v != null) v.HighlightedImage = v. HighlightedImage;

that will hint the linker to keep both getter and setter (without really executing the code).

[1] http://docs.xamarin.com/guides/ios/advanced_topics/linker/
Comment 2 Jarrod Koch 2013-11-04 11:29:20 UTC
Ah, I had forgotten about that particular functionality in the linker...thanks Sebastien!