-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Background and motivation
.NET comes with APIs that are fundamentally unsupportable without some sort of ability to run dynamic code. We already offer ability to not enable dynamic code support in some of our existing AOT form factors (Xamarin iOS without MtouchInterpreter enabled), and we have other runtime form factors on the horizon (#61231) without dynamic code support by default.
Customers currently don't have a way to tell whether a dynamic-code-less deployment option is a good fit for their app. They need to try and thoroughly test.
We need to introduce API annotations for AOT form factor. These should mirror the annotations introduced for single file and trimming in the past.
API Proposal
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
/// Indicates that the specified method requires the ability to generate new code at runtime,
/// for example through <see cref="System.Reflection"/>.
/// </summary>
/// <remarks>
/// This allows tools to understand which methods are unsafe to call when compiling ahead of time.
/// </remarks>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)]
public sealed class RequiresDynamicCodeAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="RequiresDynamicCodeAttribute"/> class
/// with the specified message.
/// </summary>
/// <param name="message">
/// A message that contains information about the usage of dynamic code.
/// </param>
public RequiresDynamicCodeAttribute(string message)
{
Message = message;
}
/// <summary>
/// Gets a message that contains information about the usage of dynamic code.
/// </summary>
public string Message { get; }
/// <summary>
/// Gets or sets an optional URL that contains more information about the method,
/// why it requries dynamic code, and what options a consumer has to deal with it.
/// </summary>
public string? Url { get; set; }
}
}
API Usage
Assume DynamicMethod
constructor was annotated with RequiresDynamicCode
:
DynamicMethod squareIt = new DynamicMethod(
"SquareIt",
typeof(long),
methodArgs,
typeof(Example).Module);
Appropriate tooling (Roslyn analyzer, AOT compiler) would generate a warnings that DynamicMethod requires dynamic code. The user would be able to either:
- Mark the method containing the callsite as
RequiresDynamicCode
. - Or, place the reference under a
RuntimeFeature.IsDynamicCodeSupported
check - Or, suppress the warning
The expectation is to mostly mirror RequiresAssemblyFilesAttribute
/RequiresUnreferencedCodeAttribute
.
Alternative Designs
No response
Risks
No response