Bug 6345 - Reflection binding information incorrect when run on device.
Summary: Reflection binding information incorrect when run on device.
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-07-31 17:27 UTC by David N. Junod
Modified: 2012-08-02 12:00 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 NOT_REPRODUCIBLE

Description David N. Junod 2012-07-31 17:27:05 UTC
I'm using sqlite-net (SQLite.cs) which uses Reflection to come up with the columns in a table.

When run on the desktop or simulator everything works fine, but when I run on an iPhone the results are different.

I have the following class, when I reflect on the device, it shows for each field CanRead=true and CanWrite=false.

public class PushAlertMessage 
{
	[PrimaryKey, AutoIncrement]
	public int _id { get; set; }

	public string Message { get; set; }

	public string Command { get; set; }

	[Indexed]
	public DateTime Received { get; set; }

	public int DisplayCount { get; set; }
}


public TableMapping (Type type)
{
	MappedType = type;
	TableName = MappedType.Name;
	var props = MappedType.GetProperties (BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty | BindingFlags.GetProperty);
	var cols = new List<Column> ();
	foreach (var p in props) {
		var ignore = p.GetCustomAttributes (typeof(IgnoreAttribute), true).Length > 0;
		if (ignore) continue;
		Console.WriteLine("[{0}] {1} r={2} w={3}", TableName, p.Name, p.CanRead, p.CanWrite);
		if (p.CanWrite || p.CanRead) {
			cols.Add (new PropColumn (p));
		}
	}
	Columns = cols.ToArray ();
	foreach (var c in Columns) {
		if (c.IsAutoInc && c.IsPK) {
			_autoPk = c;
		}
		if (c.IsPK) {
			PK = c;
		}
	}
	
	HasAutoIncPK = _autoPk != null;
}

Is there some compiler option or something that I can do to make this work like it use to (I recall it working fine in previous versions of MonoTouch).
Comment 1 David N. Junod 2012-07-31 21:15:26 UTC
I did a test program and it all works fine in the test program.  When I put the same code back into my large app, it fails.

Notice that part way through enumerating the properties (at Venue.LastSeen), it starts giving False for CanWrite.  Somehow related to the size of the codebase?

name=playground, version=0 (current version is 1)
[PushAlertMessage] _id r=True w=True
[PushAlertMessage] Message r=True w=True
[PushAlertMessage] Command r=True w=True
[PushAlertMessage] Received r=True w=True
[PushAlertMessage] DisplayCount r=True w=True
create table "PushAlertMessage" (
"_id" integer primary key autoincrement not null ,
"Message" varchar(140) ,
"Command" varchar(140) ,
"Received" datetime ,
"DisplayCount" integer )
create index if not exists "PushAlertMessage_Received" on "PushAlertMessage"("Received")
[Venue] _id r=True w=True
[Venue] Alias r=True w=True
[Venue] KeyName r=True w=True
[Venue] Name r=True w=True
[Venue] HostName r=True w=True
[Venue] Port r=True w=True
[Venue] LastSeen r=True w=False
[Venue] LastConnected r=True w=False
create table "Venue" (
"_id" integer primary key autoincrement not null ,
"Alias" varchar(140) ,
"KeyName" varchar(140) ,
"Name" varchar(140) ,
"HostName" varchar(140) ,
"Port" varchar(140) ,
"LastSeen" datetime ,
"LastConnected" datetime )
create index if not exists "Venue_Alias" on "Venue"("Alias")
create index if not exists "Venue_KeyName" on "Venue"("KeyName")
create index if not exists "Venue_Name" on "Venue"("Name")
create index if not exists "Venue_LastSeen" on "Venue"("LastSeen")
create index if not exists "Venue_LastConnected" on "Venue"("LastConnected")
[VenueHistory] _id r=True w=False
[VenueHistory] VID r=True w=False
[VenueHistory] Key r=True w=False
[VenueHistory] LastSeen r=True w=False
[VenueHistory] BSSID r=True w=False
[VenueHistory] Latitude r=True w=False
[VenueHistory] Longitude r=True w=False
create table "VenueHistory" (
"_id" integer primary key autoincrement not null ,
"VID" integer ,
"Key" varchar(140) ,
"LastSeen" datetime ,
"BSSID" varchar(140) ,
"Latitude" float ,
"Longitude" float )
create index if not exists "VenueHistory_Key" on "VenueHistory"("Key")
create index if not exists "VenueHistory_LastSeen" on "VenueHistory"("LastSeen")
create index if not exists "VenueHistory_BSSID" on "VenueHistory"("BSSID")
### OnUnhandledException : Set Method not found for 'LastSeen'
LogMinds: 2012-07-31-18:10:07|1|MT0|System.ArgumentException: Set Method not found for 'LastSeen'
  at System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00072] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoProperty.cs:354 
  at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/PropertyInfo.cs:93 
  at RealityCheck.AppShell.TableMapping+PropColumn.SetValue (System.Object obj, System.Object val) [0x00000] in /Users/djunod2/timeminds/TimeMinds/RealityCheck.AppShell/Database/SQLite/SQLite.cs:803 
  at RealityCheck.AppShell.SQLiteCommand.ExecuteQuery[Venue] (RealityCheck.AppShell.TableMapping map) [0x00000] in <filename unknown>:0 
  at RealityCheck.AppShell.SQLiteCommand.ExecuteQuery[Venue] () [0x00000] in <filename unknown>:0 
  at RealityCheck.AppShell.SQLiteDatabase.Query[Venue] (System.String query) [0x00000] in <filename unknown>:0 
  at wtkJams.DatabaseHelper.GetVenueList () [0x00006] in /Users/djunod2/rci_wtkjams/wtkJams_single/src/Database/DatabaseHelper.cs:180 
  at wtkJams.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x003bf] in /Users/djunod2/rci_wtkjams/wtkJams_single/src/Platforms/ios/Main.cs:459 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29 
  at wtkJams.Application.Main (System.String[] args) [0x00000] in /Users/djunod2/rci_wtkjams/wtkJams_single/src/Platforms/ios/Main.cs:37
Comment 2 David N. Junod 2012-08-01 06:37:18 UTC
I just installed MonoTouch 5.3.5 and ran the same test, and now it works correctly.

All is well.
Comment 3 Sebastien Pouliot 2012-08-01 07:51:09 UTC
> I did a test program and it all works fine in the test program.  When I put the
> same code back into my large app, it fails.

Could your test app be using the (default) "Link SDK" on devices ?

While your "real" application use another setting, like "Link all assemblies" for device builds ?


The linker will remove unused code (static analysis) and this includes unused 'getters' from properties. However the linker can't cover most reflection usages. So you need to help it using the [Preserve] attribute if you want to enable it on your own code (or any code that MT does not provide, i.e. SDK code).
Comment 4 David N. Junod 2012-08-02 00:10:30 UTC
I just installed MonoTouch 5.2.12 and ran the same test (my full application), and it all works correctly.

This was the debug version, it is set to "Link SDK assemblies only".

I'll use [Preserve(AllMembers=true)] on those classes just in case.
Comment 5 Sebastien Pouliot 2012-08-02 12:00:53 UTC
[Preserve] on your code should only affect using "Link all" so something else was likely happening.

If you ever see this issue again could you re-open the bug and include the build output (from MonoDevelop, Error List pad). 

Also add "-v -v" to the "additional mtouch arguments" in your project option as it might give us a few more clues about what's going on.