Bug 12208 - Referencing a property only through an expression with with linking enabled leads to a readonly property that is readonly when accessed through reflection
Summary: Referencing a property only through an expression with with linking enabled l...
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: Tools ()
Version: 6.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-05-11 15:44 UTC by Oliver Weichhold
Modified: 2013-05-12 10:31 UTC (History)
3 users (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 FEATURE

Description Oliver Weichhold 2013-05-11 15:44:12 UTC
Referencing a property only through an expression with with linking enabled leads to a readonly property that is readonly when accessed through reflection

Example:

var target = new UITextField();
Expression<Func<string>> sourcePropertyAccessor = ()=> target.Text;

var prop = typeof(UITextField).GetProperty("Text");
Debug.Assert(prop.CanWrite);
Comment 1 Sebastien Pouliot 2013-05-12 10:31:08 UTC
That's normal. The linker uses static analysis to detect what's needed by an application [1] and reflection is dynamic [2].

[1] Your expression only uses the property getter.
[2] Tools can know the string "Text" represent the property.

If you want to use reflection and linking then you need to preserve code used thru reflection. There's several ways to do it. E.g.

a. Use the setter somewhere in your code. That will allow the static analysis to find it's use and preserve the setter.

    if (some_condition_that_will_never_be_true) {
        UITextField tf;
        tf.Text = "";
    }

b. Tell the linker to preserve the setter (even if it's not used)

Since you cannot modify monotouch.dll you can't use the [Preserve] attribute so you need to use an XML file [3] that you'll pass as an argument to `mtouch` using `xml=FILE`. The content should look like this:

<linker>
        <assembly fullname="monotouch">
                <type fullname="MonoTouch.UIKit.TextField">
                        <method name="set_Text" />
                </type>
        </assembly>
</linker>

This (and other features) are covered in [4].

[3] http://spouliot.wordpress.com/2012/04/27/linker-versus-customization/
[4] http://docs.xamarin.com/guides/ios/advanced_topics/linker