Skip to content

Commit

Permalink
Add RequiresDynamicCodeAttribute to runtime (#61956)
Browse files Browse the repository at this point in the history
* Add RequiresDynamicCodeAttribute to runtime

* Delete nullable enable

* Add test file to compilation for RequiresDynamicCode and RequiresAssemblyFiles

* Fix RequiresAssemblyFiles tests
  • Loading branch information
tlakollo authored Nov 24, 2021
1 parent 773766f commit 90773ac
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@
<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" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
7 changes: 7 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9155,6 +9155,13 @@ public RequiresAssemblyFilesAttribute(string message) { }
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -155,6 +154,9 @@
<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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public void TestSetMessage(string message)
[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);
Expand All @@ -52,10 +55,13 @@ public void TestSetUrl(string url)
[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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 90773ac

Please sign in to comment.