Bug 6415 - System.Type.GetProperty
Summary: System.Type.GetProperty
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-08-06 08:46 UTC by Matthijs Koopman
Modified: 2013-06-02 12:41 UTC (History)
2 users (show)

Tags: Reflection, PropertyInfo
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 Matthijs Koopman 2012-08-06 08:46:02 UTC
Method GetProperty on an instance of System.Type will return a NULL Reference when running on a device.

When running this code:

// --> This is a sample class, so required attributes are missing for the attribute declaration
public sealed class MyAttribute : Attribute
    public int ID {get; private set; }
    public MyAttribute(int id)
        this.ID = id;

public class Foo
{ }

//Dummy call
private void DummyCall()
   Type fooType = typeof(Foo);
   PropertyInfo prop = fooType.GetProperty("ID");  // ==> Result of this will be NULL
   PropertyInfo prop = fooType.GetProperties(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(p => p.Name == "ID"); ==> Will return the corrector propertyinfo object

But in the sample above the Get method of the ID property is missing, I have to change the attribute to:

public sealed class MyAttribute : Attribute
    public int ID {get; private set; }
    public MyAttribute() : this(-1) { ; }
    public MyAttribute(int id)
        this.ID = id;

In order to make it work...
Comment 1 Sebastien Pouliot 2012-08-06 09:45:43 UTC
This is likely normal, depending on your settings and the attribute being affected. 

A quick way to confirm this is to build your project using "Linker Behavior" to "Don't link" in your project's Options, iPhone Build.

Devices builds are, by default, using the linker to remove unused code. This can drastiscally reduce your final application size and build/deploy times (which is why it's the defaut for device builds).

OTOH the static analysis done by the linker cannot cope with reflection usage. If you need some code to be kept when it's not being used (by your code) then you need to add a [Preserve] attribute. [1]

[1] http://docs.xamarin.com/ios/advanced_topics/linker#Preserving_Code
Comment 2 Sebastien Pouliot 2013-06-02 12:41:07 UTC
Closing. If you have similar issues without linking (or when preserving) please open a bug report with a test case.