Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</type>
</assembly>
<!-- The following attributes are only necessary when debugging is supported -->
<assembly fullname="System.Private.CoreLib" feature="System.Diagnostics.Debugger.IsSupported" featurevalue="false">
<assembly fullname="*" feature="System.Diagnostics.Debugger.IsSupported" featurevalue="false">
<type fullname="System.Diagnostics.DebuggableAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
Expand Down Expand Up @@ -42,13 +42,13 @@
</type>
</assembly>

<assembly fullname="System.Private.CoreLib" feature="System.Reflection.Metadata.MetadataUpdater.IsSupported" featurevalue="false">
<assembly fullname="*" feature="System.Reflection.Metadata.MetadataUpdater.IsSupported" featurevalue="false">
<type fullname="System.Reflection.Metadata.MetadataUpdateHandlerAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
</assembly>

<assembly fullname="System.Private.CoreLib" feature="System.Diagnostics.Tracing.EventSource.IsSupported" featurevalue="false">
<assembly fullname="*" feature="System.Diagnostics.Tracing.EventSource.IsSupported" featurevalue="false">
<type fullname="System.Diagnostics.Tracing.EventSourceAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
Expand All @@ -70,7 +70,7 @@
</assembly>

<!-- The following attributes are only necessary when COM is supported -->
<assembly fullname="System.Private.CoreLib" feature="System.Runtime.InteropServices.BuiltInComInterop.IsSupported" featurevalue="false">
<assembly fullname="*" feature="System.Runtime.InteropServices.BuiltInComInterop.IsSupported" featurevalue="false">
<type fullname="System.Runtime.InteropServices.ClassInterfaceAttribute">
<attribute internal="RemoveAttributeInstances" />
</type>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;

/// <summary>
/// Tests that when MetadataUpdater.IsSupported is false, the MetadataUpdateHandlerAttribute
/// instances and the handler types they reference are trimmed from all assemblies.
/// </summary>
class Program
{
static int Main(string[] args)
{
// Hard references to ensure these assemblies are loaded and not entirely removed by the trimmer.
// We use types that the trimmer cannot remove (public API surface).
_ = typeof(System.ComponentModel.TypeConverter);
_ = typeof(System.Text.Json.JsonSerializer);

// MetadataUpdater.IsSupported should be false
if (MetadataUpdater.IsSupported)
{
Console.WriteLine("Failed: MetadataUpdater.IsSupported should be false");
return -1;
}

// Verify MetadataUpdateHandlerAttribute is not present on any assembly
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var attr in assembly.GetCustomAttributesData())
{
if (attr.AttributeType.Name == "MetadataUpdateHandlerAttribute")
{
Console.WriteLine($"Failed: {assembly.GetName().Name} still has MetadataUpdateHandlerAttribute");
return -1;
}
}
}

// Verify the handler types themselves are trimmed
string[] handlerTypeNames = new[]
{
"System.Reflection.Metadata.RuntimeTypeMetadataUpdateHandler",
"System.ComponentModel.ReflectionCachesUpdateHandler",
"System.Text.Json.JsonSerializerOptionsUpdateHandler",
};

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var typeName in handlerTypeNames)
{
var type = assembly.GetType(typeName);
if (type != null)
{
Console.WriteLine($"Failed: {assembly.GetName().Name} still contains {typeName}");
return -1;
}
}
}

return 100;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
<!-- Reflection.Emit doesn't work with native AOT -->
<NativeAotIncompatible>true</NativeAotIncompatible>
</TestConsoleAppSourceFiles>
<TestConsoleAppSourceFiles Include="MetadataUpdateHandlerTrimmed.cs">
<DisabledFeatureSwitches>System.Reflection.Metadata.MetadataUpdater.IsSupported</DisabledFeatureSwitches>
</TestConsoleAppSourceFiles>
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<TestConsoleAppSourceFiles Include="UseWindowsThreadPoolFalse.cs">
Expand Down
Loading