Skip to content

Commit

Permalink
Experimental Initial Member-Level Trimming Support
Browse files Browse the repository at this point in the history
  • Loading branch information
alistairjevans committed Nov 6, 2020
1 parent 08e08bc commit e7ca457
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/Autofac/RegistrationExtensions.AssemblyScanning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ namespace Autofac
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
public static partial class RegistrationExtensions
{
private const string AssemblyScanningWarning = "Assembly Scanning is unlikely to be compatible with member-level trimming; the linker will not be able to determine which types to preserve.";

/// <summary>
/// Register all types in an assembly.
/// </summary>
/// <param name="builder">Container builder.</param>
/// <param name="assemblies">The assemblies from which to register types.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
[RequiresUnreferencedCode(AssemblyScanningWarning)]
public static IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle>
RegisterAssemblyTypes(this ContainerBuilder builder, params Assembly[] assemblies)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/RegistrationExtensions.Composite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public static IRegistrationBuilder<TService, SimpleActivatorData, SingleRegistra
/// <param name="serviceType">Service type to provide a composite for.</param>
public static IRegistrationBuilder<object, ReflectionActivatorData, DynamicRegistrationStyle> RegisterGenericComposite(
this ContainerBuilder builder,
Type compositeType,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type compositeType,
Type serviceType)
{
if (builder == null)
Expand Down
8 changes: 4 additions & 4 deletions src/Autofac/RegistrationExtensions.Decorators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static partial class RegistrationExtensions
public static IRegistrationBuilder<object, OpenGenericDecoratorActivatorData, DynamicRegistrationStyle>
RegisterGenericDecorator(
this ContainerBuilder builder,
Type decoratorType,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type decoratorType,
Type decoratedServiceType,
object fromKey,
object? toKey = null)
Expand Down Expand Up @@ -163,7 +163,7 @@ public static IRegistrationBuilder<TService, LightweightAdapterActivatorData, Dy
/// <param name="builder">Container builder.</param>
/// <param name="condition">A function that when provided with an <see cref="IDecoratorContext"/>
/// instance determines if the decorator should be applied.</param>
public static void RegisterDecorator<TDecorator, TService>(this ContainerBuilder builder, Func<IDecoratorContext, bool>? condition = null)
public static void RegisterDecorator<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TDecorator, TService>(this ContainerBuilder builder, Func<IDecoratorContext, bool>? condition = null)
where TDecorator : notnull, TService
{
if (builder == null)
Expand Down Expand Up @@ -196,7 +196,7 @@ public static void RegisterDecorator<TDecorator, TService>(this ContainerBuilder
/// instance determines if the decorator should be applied.</param>
public static void RegisterDecorator(
this ContainerBuilder builder,
Type decoratorType,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type decoratorType,
Type serviceType,
Func<IDecoratorContext, bool>? condition = null)
{
Expand Down Expand Up @@ -293,7 +293,7 @@ public static void RegisterDecorator<TService>(
/// instance determines if the decorator should be applied.</param>
public static void RegisterGenericDecorator(
this ContainerBuilder builder,
Type decoratorType,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type decoratorType,
Type serviceType,
Func<IDecoratorContext, bool>? condition = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/RegistrationExtensions.Generics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static partial class RegistrationExtensions
/// <param name="implementer">The open generic implementation type.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
public static IRegistrationBuilder<object, ReflectionActivatorData, DynamicRegistrationStyle>
RegisterGeneric(this ContainerBuilder builder, Type implementer)
RegisterGeneric(this ContainerBuilder builder, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type implementer)
{
return OpenGenericRegistrationExtensions.RegisterGeneric(builder, implementer);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Autofac/RegistrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static IRegistrationBuilder<T, SimpleActivatorData, SingleRegistrationSty
/// <param name="builder">Container builder.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
public static IRegistrationBuilder<TImplementer, ConcreteReflectionActivatorData, SingleRegistrationStyle>
RegisterType<TImplementer>(this ContainerBuilder builder)
RegisterType<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementer>(this ContainerBuilder builder)
where TImplementer : notnull
{
if (builder == null)
Expand All @@ -138,7 +138,7 @@ public static IRegistrationBuilder<TImplementer, ConcreteReflectionActivatorData
/// <param name="builder">Container builder.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
public static IRegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegistrationStyle>
RegisterType(this ContainerBuilder builder, Type implementationType)
RegisterType(this ContainerBuilder builder, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type implementationType)
{
if (builder == null)
{
Expand Down Expand Up @@ -373,7 +373,7 @@ public static IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle>
/// <param name="wiringFlags">Set wiring options such as circular dependency wiring support.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle>
PropertiesAutowired<TLimit, TActivatorData, TRegistrationStyle>(
PropertiesAutowired<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TLimit, TActivatorData, TRegistrationStyle>(
this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> registration, PropertyWiringOptions wiringFlags = PropertyWiringOptions.None)
{
if (registration == null)
Expand All @@ -396,6 +396,7 @@ public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle>
/// <param name="registration">Registration to set policy on.</param>
/// <param name="propertySelector">Policy to be used when searching for properties to inject.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
[RequiresUnreferencedCode("Autowired Properties with a custom selector may not be compatible with member-level trimming.")]
public static IRegistrationBuilder<TLimit, TActivatorData, TStyle> PropertiesAutowired<TLimit, TActivatorData, TStyle>(
this IRegistrationBuilder<TLimit, TActivatorData, TStyle> registration,
Func<PropertyInfo, object, bool> propertySelector)
Expand Down
143 changes: 143 additions & 0 deletions src/Autofac/Util/LinkerAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#pragma warning disable SA1649 // File name should match first type name
#pragma warning disable SA1402 // File may only contain a single type
#if !NET5_0

namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
/// Fake version for pre-net-5.0 targets.
/// </summary>
internal enum DynamicallyAccessedMemberTypes
{
/// <summary>
/// Specifies the default, parameterless public constructor.
/// </summary>
PublicParameterlessConstructor = 0x0001,

/// <summary>
/// Specifies all public constructors.
/// </summary>
PublicConstructors = 0x0002 | PublicParameterlessConstructor,

/// <summary>
/// Specifies all non-public constructors.
/// </summary>
NonPublicConstructors = 0x0004,

/// <summary>
/// Specifies all public methods.
/// </summary>
PublicMethods = 0x0008,

/// <summary>
/// Specifies all non-public methods.
/// </summary>
NonPublicMethods = 0x0010,

/// <summary>
/// Specifies all public fields.
/// </summary>
PublicFields = 0x0020,

/// <summary>
/// Specifies all non-public fields.
/// </summary>
NonPublicFields = 0x0040,

/// <summary>
/// Specifies all public nested types.
/// </summary>
PublicNestedTypes = 0x0080,

/// <summary>
/// Specifies all non-public nested types.
/// </summary>
NonPublicNestedTypes = 0x0100,

/// <summary>
/// Specifies all public properties.
/// </summary>
PublicProperties = 0x0200,

/// <summary>
/// Specifies all non-public properties.
/// </summary>
NonPublicProperties = 0x0400,

/// <summary>
/// Specifies all public events.
/// </summary>
PublicEvents = 0x0800,

/// <summary>
/// Specifies all non-public events.
/// </summary>
NonPublicEvents = 0x1000,
}

/// <summary>
/// Fake attribute for older versions.
/// </summary>
[AttributeUsage(
AttributeTargets.Field | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter |
AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Method,
Inherited = false)]
internal sealed class DynamicallyAccessedMembersAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="DynamicallyAccessedMembersAttribute"/> class
/// with the specified member types.
/// </summary>
/// <param name="memberTypes">The types of members dynamically accessed.</param>
public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes)
{
MemberTypes = memberTypes;
}

/// <summary>
/// Gets the <see cref="DynamicallyAccessedMemberTypes"/> which specifies the type
/// of members dynamically accessed.
/// </summary>
public DynamicallyAccessedMemberTypes MemberTypes { get; }
}

/// <summary>
/// Indicates that the specified method requires dynamic access to code that is not referenced
/// statically, for example through <see cref="System.Reflection"/>.
/// </summary>
/// <remarks>
/// This allows tools to understand which methods are unsafe to call when removing unreferenced
/// code from an application.
/// </remarks>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)]
public sealed class RequiresUnreferencedCodeAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="RequiresUnreferencedCodeAttribute"/> class
/// with the specified message.
/// </summary>
/// <param name="message">
/// A message that contains information about the usage of unreferenced code.
/// </param>
public RequiresUnreferencedCodeAttribute(string message)
{
Message = message;
}

/// <summary>
/// Gets a message that contains information about the usage of unreferenced code.
/// </summary>
public string Message { get; }

/// <summary>
/// Gets or sets an optional URL that contains more information about the method,
/// why it requries unreferenced code, and what options a consumer has to deal with it.
/// </summary>
public string? Url { get; set; }
}
}

#endif

0 comments on commit e7ca457

Please sign in to comment.