Skip to content

Commit 90773ac

Browse files
authored
Add RequiresDynamicCodeAttribute to runtime (#61956)
* Add RequiresDynamicCodeAttribute to runtime * Delete nullable enable * Add test file to compilation for RequiresDynamicCode and RequiresAssemblyFiles * Fix RequiresAssemblyFiles tests
1 parent 773766f commit 90773ac

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
lines changed

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@
254254
<Compile Include="$(MSBuildThisFileDirectory)System\DefaultBinder.cs" />
255255
<Compile Include="$(MSBuildThisFileDirectory)System\Delegate.cs" />
256256
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresAssemblyFilesAttribute.cs" />
257+
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttribute.cs" />
257258
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
258259
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
259260
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.Diagnostics.CodeAnalysis
5+
{
6+
/// <summary>
7+
/// Indicates that the specified method requires the ability to generate new code at runtime,
8+
/// for example through <see cref="System.Reflection"/>.
9+
/// </summary>
10+
/// <remarks>
11+
/// This allows tools to understand which methods are unsafe to call when compiling ahead of time.
12+
/// </remarks>
13+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)]
14+
public sealed class RequiresDynamicCodeAttribute : Attribute
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="RequiresDynamicCodeAttribute"/> class
18+
/// with the specified message.
19+
/// </summary>
20+
/// <param name="message">
21+
/// A message that contains information about the usage of dynamic code.
22+
/// </param>
23+
public RequiresDynamicCodeAttribute(string message)
24+
{
25+
Message = message;
26+
}
27+
28+
/// <summary>
29+
/// Gets a message that contains information about the usage of dynamic code.
30+
/// </summary>
31+
public string Message { get; }
32+
33+
/// <summary>
34+
/// Gets or sets an optional URL that contains more information about the method,
35+
/// why it requires dynamic code, and what options a consumer has to deal with it.
36+
/// </summary>
37+
public string? Url { get; set; }
38+
}
39+
}

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9155,6 +9155,13 @@ public RequiresAssemblyFilesAttribute(string message) { }
91559155
public string? Message { get { throw null; } }
91569156
public string? Url { get { throw null; } set { } }
91579157
}
9158+
[System.AttributeUsageAttribute(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited = false)]
9159+
public sealed partial class RequiresDynamicCodeAttribute : System.Attribute
9160+
{
9161+
public RequiresDynamicCodeAttribute(string message) { }
9162+
public string Message { get { throw null; } }
9163+
public string? Url { get { throw null; } set { } }
9164+
}
91589165
[System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
91599166
public sealed partial class RequiresUnreferencedCodeAttribute : System.Attribute
91609167
{

src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
<Compile Include="System\DBNullTests.cs" />
5959
<Compile Include="System\DecimalTests.cs" />
6060
<Compile Include="System\DelegateTests.cs" />
61-
<Compile Include="System\Diagnostics\CodeAnalysis\DynamicDependencyAttributeTests.cs" />
6261
<Compile Include="System\DivideByZeroExceptionTests.cs" />
6362
<Compile Include="System\DoubleTests.cs" />
6463
<Compile Include="System\DuplicateWaitObjectExceptionTests.cs" />
@@ -155,6 +154,9 @@
155154
<Compile Include="System\ComponentModel\EditorBrowsableAttributeTests.cs" />
156155
<Compile Include="System\Diagnostics\ConditionalAttributeTests.cs" />
157156
<Compile Include="System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttributeTests.cs" />
157+
<Compile Include="System\Diagnostics\CodeAnalysis\DynamicDependencyAttributeTests.cs" />
158+
<Compile Include="System\Diagnostics\CodeAnalysis\RequiresAssemblyFilesAttributeTests.cs" />
159+
<Compile Include="System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttributeTests.cs" />
158160
<Compile Include="System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttributeTests.cs" />
159161
<Compile Include="System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttributeTests.cs" />
160162
<Compile Include="System\Diagnostics\StackTraceHiddenAttributeTests.cs" />

src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresAssemblyFilesAttributeTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public void TestSetMessage(string message)
3434
[InlineData(null)]
3535
public void TestSetUrl(string url)
3636
{
37-
var attr = new RequiresAssemblyFilesAttribute(Url = url);
37+
var attr = new RequiresAssemblyFilesAttribute()
38+
{
39+
Url = url
40+
};
3841

3942
Assert.Null(attr.Message);
4043
Assert.Equal(url, attr.Url);
@@ -52,10 +55,13 @@ public void TestSetUrl(string url)
5255
[InlineData(null, null)]
5356
public void TestSetMessageAndUrl(string message, string url)
5457
{
55-
var attr = new RequiresAssemblyFilesAttribute(message, Url = url);
58+
var attr = new RequiresAssemblyFilesAttribute(message)
59+
{
60+
Url = url
61+
};
5662

5763
Assert.Equal(message, attr.Message);
58-
Assert.Equal(ur, attr.Url);
64+
Assert.Equal(url, attr.Url);
5965
}
6066
}
6167
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
6+
namespace System.Diagnostics.CodeAnalysis.Tests
7+
{
8+
public class RequiresDynamicCodeAttributeTests
9+
{
10+
[Fact]
11+
public void TestConstructor()
12+
{
13+
var attr = new RequiresDynamicCodeAttribute("User Message");
14+
15+
Assert.Equal("User Message", attr.Message);
16+
Assert.Null(attr.Url);
17+
}
18+
19+
[Theory]
20+
[InlineData("https://dot.net")]
21+
[InlineData("")]
22+
[InlineData(null)]
23+
public void TestSetUrl(string url)
24+
{
25+
var attr = new RequiresDynamicCodeAttribute("User Message")
26+
{
27+
Url = url
28+
};
29+
30+
Assert.Equal("User Message", attr.Message);
31+
Assert.Equal(url, attr.Url);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)