Bug 4848 - Type.GetConstructors fails to return default constructor on device
Summary: Type.GetConstructors fails to return default constructor on device
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.2
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-05-02 19:05 UTC by Nathan Curtis
Modified: 2012-05-02 21:31 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 Nathan Curtis 2012-05-02 19:05:23 UTC
Using MonoTouch 5.2.11, I have a test case.

	public class WithDefaultCtor
		public String GetStatement ()
			return "I have no declared ctor!";

Tested with this code:

			Type t = typeof(WithDefaultCtor);
			System.Console.WriteLine("ctors: " + t.GetConstructors().Length);

In the simulator, as expected, the output is "ctors: 1", representing the default constructor. On the device (iPad), the result is "ctors: 0".
Comment 1 Nathan Curtis 2012-05-02 19:28:35 UTC
An added note I just remembered that might be relevant: I recently had to turn on "Link all assemblies" in Linker Behavior in the project options. I had to do that because I hit this bug, and it was the recommended fix: https://bugzilla.xamarin.com/show_bug.cgi?id=1102
Comment 2 Nathan Curtis 2012-05-02 20:57:24 UTC
I'm now convinced that Link all assemblies is to cause for this behavior, which does indeed make some sense. You just can't link all assemblies and rely on some methods only being called by reflection. I found another problem with my project today that was also caused by some needed methods getting stripped out.

Fortunately, I do have steps I can take to fairly easily break some of our assemblies apart, but that may be just a stopgap. I'm wondering if any further progress has been made on that issue; I'll ask in a comment on that bug too.
Comment 3 Sebastien Pouliot 2012-05-02 21:31:24 UTC
"Link all" will remove any "unused", from a static analysis point of view, code from every assembly. So yes the behavior is normal if the .ctor is only used for reflection (as this can't be seen by static analysis).

The easiest way (for 5.2.*) to workaround this is to add the [Preserve] attribute on the constructor (or any code that you want the linker to keep even if it *seems* unused).

More information is available on: http://docs.xamarin.com/ios/advanced_topics/linker

MonoTouch 5.3.3+ allows you to use XML files to customize the linker (and avoid adding attributes inside your code). See: http://spouliot.wordpress.com/2012/04/27/linker-versus-customization/