Bug 6179 - DataMemberAttribute.Order in System.Runtime.Serialization
Summary: DataMemberAttribute.Order in System.Runtime.Serialization
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 4.x
Hardware: Other Other
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-07-18 12:09 UTC by Martin Stafford
Modified: 2012-07-18 13:46 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 Martin Stafford 2012-07-18 12:09:27 UTC

Comment 1 Martin Stafford 2012-07-18 12:23:55 UTC
The Order property of DataMemberAttribute is write-only when run on the iPhone device iOS version 4.2.1, hence the line:

return ((PropertyInfo)members[0]).GetValue(attrib, null);


'members[0]' is the "Order" property of the DataMemberAttribute class in this case.  Here's more context:

        private static object GetMemberValue(Attribute attrib, string memberName)
            MemberInfo[] members = attrib.GetType().GetMember(memberName, BindingFlags.Public | BindingFlags.Instance);
            if (members.Length != 1) return null;
            switch (members[0].MemberType)
                case MemberTypes.Property:
                    return ((PropertyInfo)members[0]).GetValue(attrib, null);
                case MemberTypes.Field:
                    return ((FieldInfo)members[0]).GetValue(attrib);
                case MemberTypes.Method:
                    return ((MethodInfo)members[0]).Invoke(attrib, null);
            return null;

The code works file on the iPhone Simulator on a Mac
Comment 2 Sebastien Pouliot 2012-07-18 13:46:32 UTC
This is because the managed linker removes unused code (using static analysis) and is (by default) enabled on the device and not for the simulator (this results in the fastest build time on each case). 

However static analysis cannot handle cases where reflection is being used. For such cases you can:

1. turn off linking (not recommended since you'll end up with a very large application size); or

2. explicitely tell the linker what you need. 

Most of the time you can do this with the [Preserve] attributes but in this case [DataMember] is part of the SDK (not your own code). You can still give an "hint" to the linker by adding some code (that won't be linked away) inside your application, e.g:

DataMemberAttribute dma = new DataMemberAttribute ();
dma.Order = dma.Order;

That will ensure the linker will mark (and keep) both the getter and setter.