From c0b0ac14aad45048ef91eadfa2d8962af905aa0e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 18 Jan 2023 17:34:34 +0100 Subject: [PATCH 1/3] Add RequiresDynamicCodeAttribute polyfill --- ...deAnalysis.RequiresDynamicCodeAttribute.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/PolySharp.SourceGenerators/EmbeddedResources/RuntimeSupported/System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute.cs diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/RuntimeSupported/System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/RuntimeSupported/System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute.cs new file mode 100644 index 0000000..f05322b --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/RuntimeSupported/System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute.cs @@ -0,0 +1,47 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// Indicates that the specified method requires the ability to generate new code at runtime, + /// for example through . + /// + /// + /// This allows tools to understand which methods are unsafe to call when compiling ahead of time. + /// + [global::System.AttributeUsage( + global::System.AttributeTargets.Method | + global::System.AttributeTargets.Constructor | + global::System.AttributeTargets.Class, + Inherited = false)] + internal sealed class RequiresDynamicCodeAttribute : global::System.Attribute + { + /// + /// Initializes a new instance of the class + /// with the specified message. + /// + /// + /// A message that contains information about the usage of dynamic code. + /// + public RequiresDynamicCodeAttribute(string message) + { + Message = message; + } + + /// + /// Gets a message that contains information about the usage of dynamic code. + /// + public string Message { get; } + + /// + /// Gets or sets an optional URL that contains more information about the method, + /// why it requires dynamic code, and what options a consumer has to deal with it. + /// + public string? Url { get; set; } + } +} \ No newline at end of file From e42449591d720de93d04b9ccd9e29ffe07f34a5c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 18 Jan 2023 17:38:02 +0100 Subject: [PATCH 2/3] Add [RequiresDynamicCode] to readme files --- README.md | 1 + src/PolySharp.Package/README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 1028dd8..0f21d35 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ It also includes the following optional runtime-supported polyfills: - `[DynamicallyAccessedMembers]` - `[DynamicDependency]` - `[RequiresUnreferencedCode]` + - `[RequiresDynamicCode]` - `[UnconditionalSuppressMessage]` - `[StackTraceHidden]` (see [here](https://makolyte.com/csharp-exclude-exception-throw-helper-methods-from-the-stack-trace/)) - `[UnmanagedCallersOnly]` (see [docs](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute))) diff --git a/src/PolySharp.Package/README.md b/src/PolySharp.Package/README.md index f266bd4..bb1e76d 100644 --- a/src/PolySharp.Package/README.md +++ b/src/PolySharp.Package/README.md @@ -55,6 +55,7 @@ It also includes the following optional runtime-supported polyfills: - `[DynamicallyAccessedMembers]` - `[DynamicDependency]` - `[RequiresUnreferencedCode]` + - `[RequiresDynamicCode]` - `[UnconditionalSuppressMessage]` - `[StackTraceHidden]` (see [here](https://makolyte.com/csharp-exclude-exception-throw-helper-methods-from-the-stack-trace/)) - `[UnmanagedCallersOnly]` (see [docs](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute))) From c0b30ec00d895607bc10b4eb24962b4beed6f7f0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 18 Jan 2023 17:38:11 +0100 Subject: [PATCH 3/3] Add unit test method for [RequiresDynamicCode] --- tests/PolySharp.Tests/RuntimeSupport.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/PolySharp.Tests/RuntimeSupport.cs b/tests/PolySharp.Tests/RuntimeSupport.cs index ea6ac91..05c729e 100644 --- a/tests/PolySharp.Tests/RuntimeSupport.cs +++ b/tests/PolySharp.Tests/RuntimeSupport.cs @@ -71,4 +71,9 @@ public void SuppressEverything(Type type) { _ = type.GetProperties(); } + + [RequiresDynamicCode("This method creates some type at runtime")] + public void MakeUpSomeNewType() + { + } } \ No newline at end of file