Bug 16299 - Linker removes TypeConverter attribute
Summary: Linker removes TypeConverter attribute
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Tools and Addins ()
Version: 4.10.1
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Alex Rønne Petersen
URL:
Depends on:
Blocks:
 
Reported: 2013-11-16 07:37 UTC by Grigory (Playtika)
Modified: 2013-11-21 10:32 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Repro project (14.84 KB, application/octet-stream)
2013-11-16 07:37 UTC, Grigory (Playtika)
Details


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 FIXED

Description Grigory (Playtika) 2013-11-16 07:37:59 UTC
Created attachment 5457 [details]
Repro project

Linker mode - All assemblies.
This issue reproduces if type and its converter are in referenced assembly. 

I've added repro project for Android.

Here is the code i try to execute:

var converter = TypeDescriptor.GetConverter (new MyClass());
var attributes = typeof(MyClass).GetCustomAttributes (false);

if (!(converter is MyClassConverter) || attributes.Length == 0) 
{
	Console.WriteLine ("Bug");
}
-----------------------------------------------------------------			
	[TypeConverter(typeof(MyClassConverter))]
	public class MyClass
	{
		public MyClass ()
		{
		}
	}

	public class MyClassConverter : ArrayConverter 
	{
	}
-----------------------------------------------------------------
Comment 1 Grigory (Playtika) 2013-11-16 07:40:53 UTC
Xamarin.Android
Version: 4.10.1 (Enterprise Edition)
Comment 2 Grigory (Playtika) 2013-11-16 07:54:43 UTC
*OFFTOPIC*
I see a label at the top of the page that says that guys with enterprise license should use some another way of issue reporting. But in my account i see not special "support email" which i should write to. There is a page with connected computers, with pasword changing logic, but no support email.

Is this a but in account page ?
*/OFFTOPIC*
Comment 4 Sebastien Pouliot 2013-11-18 21:11:50 UTC
The issue is that `Activator.CreateInstance` is used on the `typeof(MyClassConverter)` to create the converter. The use of reflection means that linker lose track of need for the default .ctor (i.e. it does not get marked, nor kept in the final application).

I'll add some additional logic to detect this specific condition. In the mean time there's several workaround possible. 

Since this is your code the easiest one is to manually preserve the default constructor using the [Preserve] attribute. E.g.

	public class CustomConverter : TypeConverter {
		[Preserve (Conditional = true)]
		public CustomConverter ()
		{
		}
	}

The above example instruct the linker to keep the constructor (or any decorated method) if it's type is marked (i.e. if the linker preserve the type for any reason). 

An XML file can also be used to achieve the same result (e.g. if code cannot be modified).

Finally you could use "Link SDK", as this won't modify your code, but you'll lose the optimizations that can be done on some (non-SDK) assemblies.
Comment 5 Grigory (Playtika) 2013-11-19 03:22:30 UTC
Hi Sebastien, thanks for you answer.

The issue is not in ctor. It is preserved. Please look at the repro project i've attached. I preserve ctor in XML config file.

Issue is that Attribute is removed from type.
Issue actually is that i can MAKE linker preserve fields/method/ctors but i CAN'T MAKE it preserve attributes.
Comment 6 Sebastien Pouliot 2013-11-19 12:02:16 UTC
Sorry. I do not work on the Android product (but on both iOS and OSX linkers) so what I've seen differed. 

The removal of the [TypeConverter] attribute is not done on iOS or OSX - so only the .ctor issue remained.

Short story: The linker does remove attributes that are *not* needed at runtime but the [TypeConverter] attribute should not be one of them.


note: As for the need to preserve the TypeConverter default ctor it's already fixed in OSX [1] and will some be merged for iOS.

[1] xamcore 838bb81fb1901603ff7fe49749de5b0aaa70a2b6
Comment 7 Alex Rønne Petersen 2013-11-21 10:32:28 UTC
This is fixed, and will be included in the next release made from master.