Bug 28389 - Custom [Preserve] attribute in PCL only works under the Foundation namespace for iOS
Summary: Custom [Preserve] attribute in PCL only works under the Foundation namespace ...
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 8.8.0
Hardware: PC Windows
: --- normal
Target Milestone: 8.10
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2015-03-24 18:24 UTC by Jon Douglas [MSFT]
Modified: 2015-03-24 20:50 UTC (History)
1 user (show)

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


Attachments
Reproduction (2.96 MB, application/octet-stream)
2015-03-24 18:24 UTC, Jon Douglas [MSFT]
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 Jon Douglas [MSFT] 2015-03-24 18:24:04 UTC
Created attachment 10483 [details]
Reproduction

*Description

Using "Link All Assemblies", a custom [Preserve] attribute is added to the class and ctor(Constructor) of the injected classes to ensure they are not linked away. Since this is a PCL, there is no access to the Foundation.PreserveAttribute and thus we define a PreserveAttribute as the following:

using System;

namespace Own
{
    [AttributeUsage(
        AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum |
        AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field |
        AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate)]
    public sealed class PreserveAttribute : Attribute
    {
        public bool AllMembers;
        public bool Conditional;
    }
}

However once defining this PreserveAttribute to classes we want to "Preserve", they are still stripped away unless the attribute is added to the "Foundation" namespace instead of the "Own" namespace. I believe that the namespace should be irrelevant to the case, but this seems to be an exception.

EX:

using System;

namespace Foundation
{
    [AttributeUsage(
        AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum |
        AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field |
        AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate)]
    public sealed class PreserveAttribute : Attribute
    {
        public bool AllMembers;
        public bool Conditional;
    }
}

In which if our Class is decorated via the following:

    [Own.Preserve]
    public class Dummy3 : IDummy3
    {
        [Own.Preserve]
        public Dummy3()
        {
            DoNothing();
        }

        /// <summary>
        /// Runs FittingBLOB deserialization and returns a string explaining the result.
        /// </summary>
        /// <returns>A string explaining the result.</returns>
        public async Task<string> InterpretFittingBlob()
        {
            string result = await Task.Run(() => "hello");
            return result;
        }

        private void DoNothing() { }
    }

Observations: This class will not be found at runtime unless we change the namespace of the PreserveAttribute to Foundation as depicted above.

*Workarounds

Currently there are a couple workarounds that I know of:

1) Apply a [assembly: Own.Preserve] at the AssemblyInfo.cs level.
2) Use --linkskip on the assembly

*Version Information

Microsoft Visual Studio Ultimate 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.5.51641

Installed Version: Ultimate

Architecture and Modeling Tools   06181-004-0451031-02320
Microsoft Architecture and Modeling Tools
    
UML® and Unified Modeling Language™ are trademarks or registered trademarks of the Object Management Group, Inc. in the United States and other countries.

LightSwitch for Visual Studio 2013   06181-004-0451031-02320
Microsoft LightSwitch for Visual Studio 2013

Team Explorer for Visual Studio 2013   06181-004-0451031-02320
Microsoft Team Explorer for Visual Studio 2013

Visual Basic 2013   06181-004-0451031-02320
Microsoft Visual Basic 2013

Visual C# 2013   06181-004-0451031-02320
Microsoft Visual C# 2013

Visual C++ 2013   06181-004-0451031-02320
Microsoft Visual C++ 2013

Visual F# 2013   06181-004-0451031-02320
Microsoft Visual F# 2013

Visual Studio 2013 Code Analysis Spell Checker   06181-004-0451031-02320
Microsoft® Visual Studio® 2013 Code Analysis Spell Checker

Portions of International CorrectSpell™ spelling correction system © 1993 by Lernout & Hauspie Speech Products N.V. All rights reserved.

The American Heritage® Dictionary of the English Language, Third Edition Copyright © 1992 Houghton Mifflin Company. Electronic version licensed from Lernout & Hauspie Speech Products N.V. All rights reserved.

Windows Phone SDK 8.0 - ENU   06181-004-0451031-02320
Windows Phone SDK 8.0 - ENU

ApiPortVS   1.0
API portability analyzer.

ASP.NET and Web Tools   12.4.51016.0
Microsoft Web Developer Tools contains the following components:
Support for creating and opening ASP.NET web projects
Browser Link: A communication channel between Visual Studio and browsers
Editor extensions for HTML, CSS, and JavaScript
Page Inspector: Inspection tool for ASP.NET web projects
Scaffolding: A framework for building and running code generators
Server Explorer extensions for Microsoft Azure Websites
Web publishing: Extensions for publishing ASP.NET web projects to hosting providers, on-premises servers, or Microsoft Azure

ASP.NET Web Frameworks and Tools 2012.2   4.1.21001.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.21010.0
For additional information, visit http://www.asp.net/

AWS Toolkit for Visual Studio 2013   1.6.4.1
AWS Toolkit for Visual Studio 2013.
Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.

This software includes third party software subject to the following copyrights:
- WPF Toolkit for charting and grid controls, Microsoft Public License (Ms-PL) 
  [http://wpf.codeplex.com/license]
- Logging from log4net, Apache License 
  [http://logging.apache.org/log4net/license.html]
- Putty for PPK to PEM conversion, MIT license 
  [http://www.chiark.greenend.org.uk/~sgtatham/putty/licence.html]
- NGit for AWS Elastic Beanstalk incremental push
  [https://github.com/mono/ngit/blob/master/NGit.license.txt]
- NSch dependency for NGit
  [https://github.com/mono/ngit/blob/master/NSch.license.txt]
- Sharpen dependency for NGit
  [https://github.com/mono/ngit/blob/master/Sharpen/AssemblyInfo.cs]
- ICSharpCode.SharpZipLib dependency for NGit
  [http://www.icsharpcode.net/opensource/sharpziplib/]
- Mono.Posix.dll and Mono.Security.dll dependencies for NGit
  [http://mono-project.com/FAQ:_Licensing#Licensing]
- MPFProj for Visual Studio Project Framework
  [http://mpfproj10.codeplex.com/license]
- JSONLint for JSON validation
  [https://github.com/zaach/jsonlint/blob/master/README.md]
- Noesis.Javascript.dll for JSON validation
  [http://javascriptdotnet.codeplex.com/license]

Color Theme Designer   1.0
Designer for creating new color themes

Common Azure Tools   1.3
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Entity Framework Power Tools   1.0
Adds useful design-time DbContext features to the Visual Studio Solution Explorer context menu. 

When right-clicking on a file containing a derived DbContext class, the following context menu functions are supported:

1) View Entity Data Model - Displays the underlying Code First model in the Entity Framework designer.
2) View Entity Data Model XML - Displays the EDMX XML representing the underlying Code First model.
3) Generate Views - Generates pre-compiled views used by the EF runtime to improve start-up performance. Adds the generated views file to the containing project.

File Nesting   1.0
Automatically nest files based on file name and enables developers to nest and unnest any file manually

JetBrains ReSharper 8.2   Full Edition build 8.2.0.2160 on 2014-03-21T01:53:08
JetBrains ReSharper 8.2 package for Microsoft Visual Studio. For more information about ReSharper, visit http://www.jetbrains.com/resharper/. Copyright © 2003–2015 JetBrains, Inc.

Microsoft Advertising SDK for Windows Phone   
Microsoft Advertising SDK for Windows Phone
Build 

Microsoft Azure Mobile Services Tools   1.3
Microsoft Azure Mobile Services Tools

Microsoft XNA Game Studio   4.0
Microsoft XNA Game Studio 4.0
Build 4.0.30901.0

NuGet Package Manager   2.8.50926.663
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PowerShell Tools   1.3
Provides file classification services using PowerShell

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

Release Management for Visual Studio Package   1.0
Release Management for Visual Studio

SAPReferenceManager   1.0
Manages References to Shared Projects

SQL Server Data Tools   12.0.41012.0
Microsoft SQL Server Data Tools

Windows Azure Tools   2.3
Windows Azure Tools for Microsoft Visual Studio 2013 - v2.3.20320.1602

Windows Phone 8.1 SDK Integration   1.0
This package integrates the tools for the Windows Phone 8.1 SDK into the menus and controls of Visual Studio.

Xamarin   3.9.483.0 (978be3d)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   4.20.0.37 (9e05e39f02bafe8fc0b7ab025d99f3b446835ad4)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS   8.8.0.0 (702be5207a1446b066f4653f68739da13c045227)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified

XNA Game Studio 4.0   1.0
Information about my package

=====================
Please let me know if anything else is needed.
Comment 1 Sebastien Pouliot 2015-03-24 20:10:36 UTC
It looks like the type `Dummy3` is being preserved (as expected) but not it's ctor - even if it has the same custom/user attribute.
Comment 2 Sebastien Pouliot 2015-03-24 20:14:17 UTC
> Currently there are a couple workarounds that I know of:

3. Use an XML file [1] to describe what needs to be preserved. It's more precise than skipping (better saving) but less easy to construct (file) and configure (like --linkskip it must be set in every project configuration)

[1] https://spouliot.wordpress.com/2012/04/27/linker-versus-customization/
Comment 3 Sebastien Pouliot 2015-03-24 20:50:51 UTC
There was indeed a place where a namespace check was done, that's why it worked with Foundation namespace (which can be used as a workaround until 8.10 is released).

Fixed in master / f02b611c608149005e4a8dacf6ff34ee154694e8
and macios-cycle5 / 90de7df4700ee59137b63fc6f1f4bf2c60ea018e