Add RequiresDynamicCodeAttribute to runtime (#61956)
authorTlakaelel Axayakatl Ceja <tlakaelel.ceja@microsoft.com>
Wed, 24 Nov 2021 11:53:01 +0000 (03:53 -0800)
committerGitHub <noreply@github.com>
Wed, 24 Nov 2021 11:53:01 +0000 (06:53 -0500)
* Add RequiresDynamicCodeAttribute to runtime

* Delete nullable enable

* Add test file to compilation for RequiresDynamicCode and RequiresAssemblyFiles

* Fix RequiresAssemblyFiles tests

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs [new file with mode: 0644]
src/libraries/System.Runtime/ref/System.Runtime.cs
src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj
src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresAssemblyFilesAttributeTests.cs
src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttributeTests.cs [new file with mode: 0644]

index 9723fc8..8a5bf95 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)System\DefaultBinder.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Delegate.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresAssemblyFilesAttribute.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttribute.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs
new file mode 100644 (file)
index 0000000..a067002
--- /dev/null
@@ -0,0 +1,39 @@
+// 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
+{
+    /// <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 requires dynamic code, and what options a consumer has to deal with it.
+        /// </summary>
+        public string? Url { get; set; }
+    }
+}
index 04b63cb..a03a42d 100644 (file)
@@ -9155,6 +9155,13 @@ namespace System.Diagnostics.CodeAnalysis
         public string? Message { get { throw null; } }
         public string? Url { get { throw null; } set { } }
     }
+    [System.AttributeUsageAttribute(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited = false)]
+    public sealed partial class RequiresDynamicCodeAttribute : System.Attribute
+    {
+        public RequiresDynamicCodeAttribute(string message) { }
+        public string Message { get { throw null; } }
+        public string? Url { get { throw null; } set { } }
+    }
     [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
     public sealed partial class RequiresUnreferencedCodeAttribute : System.Attribute
     {
index 913da87..734e649 100644 (file)
@@ -58,7 +58,6 @@
     <Compile Include="System\DBNullTests.cs" />
     <Compile Include="System\DecimalTests.cs" />
     <Compile Include="System\DelegateTests.cs" />
-    <Compile Include="System\Diagnostics\CodeAnalysis\DynamicDependencyAttributeTests.cs" />
     <Compile Include="System\DivideByZeroExceptionTests.cs" />
     <Compile Include="System\DoubleTests.cs" />
     <Compile Include="System\DuplicateWaitObjectExceptionTests.cs" />
     <Compile Include="System\ComponentModel\EditorBrowsableAttributeTests.cs" />
     <Compile Include="System\Diagnostics\ConditionalAttributeTests.cs" />
     <Compile Include="System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttributeTests.cs" />
+    <Compile Include="System\Diagnostics\CodeAnalysis\DynamicDependencyAttributeTests.cs" />
+    <Compile Include="System\Diagnostics\CodeAnalysis\RequiresAssemblyFilesAttributeTests.cs" />
+    <Compile Include="System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttributeTests.cs" />
     <Compile Include="System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttributeTests.cs" />
     <Compile Include="System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttributeTests.cs" />
     <Compile Include="System\Diagnostics\StackTraceHiddenAttributeTests.cs" />
index 974de11..6dd40bf 100644 (file)
@@ -34,7 +34,10 @@ namespace System.Diagnostics.CodeAnalysis.Tests
         [InlineData(null)]
         public void TestSetUrl(string url)
         {
-            var attr = new RequiresAssemblyFilesAttribute(Url = url);
+            var attr = new RequiresAssemblyFilesAttribute()
+            {
+                Url = url
+            };
 
             Assert.Null(attr.Message);
             Assert.Equal(url, attr.Url);
@@ -52,10 +55,13 @@ namespace System.Diagnostics.CodeAnalysis.Tests
         [InlineData(null, null)]
         public void TestSetMessageAndUrl(string message, string url)
         {
-            var attr = new RequiresAssemblyFilesAttribute(message, Url = url);
+            var attr = new RequiresAssemblyFilesAttribute(message)
+            {
+                Url = url
+            };
 
             Assert.Equal(message, attr.Message);
-            Assert.Equal(ur, attr.Url);
+            Assert.Equal(url, attr.Url);
         }
     }
 }
diff --git a/src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttributeTests.cs b/src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttributeTests.cs
new file mode 100644 (file)
index 0000000..a9cbc6f
--- /dev/null
@@ -0,0 +1,34 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using Xunit;
+
+namespace System.Diagnostics.CodeAnalysis.Tests
+{
+    public class RequiresDynamicCodeAttributeTests
+    {
+        [Fact]
+        public void TestConstructor()
+        {
+            var attr = new RequiresDynamicCodeAttribute("User Message");
+
+            Assert.Equal("User Message", attr.Message);
+            Assert.Null(attr.Url);
+        }
+
+        [Theory]
+        [InlineData("https://dot.net")]
+        [InlineData("")]
+        [InlineData(null)]
+        public void TestSetUrl(string url)
+        {
+            var attr = new RequiresDynamicCodeAttribute("User Message")
+            {
+                Url = url
+            };
+
+            Assert.Equal("User Message", attr.Message);
+            Assert.Equal(url, attr.Url);
+        }
+    }
+}