Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequiresUnreferencedCode does not seem to work at the class level #18720

Closed
yaakov-h opened this issue Jul 4, 2021 · 2 comments
Closed

RequiresUnreferencedCode does not seem to work at the class level #18720

yaakov-h opened this issue Jul 4, 2021 · 2 comments
Labels
untriaged Request triage from a team member

Comments

@yaakov-h
Copy link
Member

yaakov-h commented Jul 4, 2021

Given the following code, with .NET SDK 6.0.100-preview.5.21302.13:

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace DemoTrimmedApp
{
    class Program
    {
        static void Main()
        {
            foreach (var type in SuperHandyReflectionUtil.GetTypeNames())
            {
                Console.WriteLine(type);
            }
        }
    }

    class EligibleForTrimming { }

    class AlsoEligibleForTrimming { }

    [RequiresUnreferencedCode("pretending for a moment that we do something more dynamic")]
    class SuperHandyReflectionUtil
    {
        public static IEnumerable<string> GetTypeNames()
        {
            foreach (var type in Assembly.GetCallingAssembly().GetTypes())
            {
                yield return type.Name;
            }
        }
    }
}

and given this simple project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <PublishTrimmed>true</PublishTrimmed>
    <PublishSingleFile>true</PublishSingleFile>
  </PropertyGroup>

</Project>

I would expect that there would be no warnings from within SuperHandyReflectionUtil about using reflection, and a warning in Program.Main that I am calling code that has the RequiresUnreferencedCode attribute.

Instead, I get the following singular warning, which is the exact same warning I get when RequiresUnreferencedCode is not present at all (i.e when deleted or commented out):

warning IL2026: Using method 'System.Reflection.Assembly.GetTypes()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.

It would appear that even though the attribute has AttributeTargets.Class, it only has an effect when applied to every individual method or constructor within said class that requires it.

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Request triage from a team member label Jul 4, 2021
@vitek-karas
Copy link
Member

This is a dupe of dotnet/linker#1742. It's currently being worked on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

2 participants