Skip to content

Commit 9a391bc

Browse files
[mono] Do not throw in AssemblyExtensions.TryGetRawMetadata. (#96370)
* [mono] Do not throw in AssemblyExtensions.TryGetRawMetadata. * Add a test for `AssemblyExtensions.TryGetRawMetadata`. * Try to read the assembly metadata blob in the test.
1 parent a98d9f0 commit 9a391bc

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
using System.Reflection.Metadata;
6+
using System.Reflection;
7+
8+
namespace System.Runtime.Loader.Tests
9+
{
10+
public unsafe class AssemblyExtensionsTest
11+
{
12+
[Fact]
13+
public void TryGetRawMetadata()
14+
{
15+
bool supportsRawMetadata = PlatformDetection.IsNotMonoRuntime && PlatformDetection.IsNotNativeAot;
16+
17+
Assembly assembly = typeof(AssemblyExtensionsTest).Assembly;
18+
bool hasMetadata = assembly.TryGetRawMetadata(out byte* blob, out int length);
19+
20+
Assert.Equal(supportsRawMetadata, hasMetadata);
21+
Assert.Equal(supportsRawMetadata, blob != null);
22+
Assert.Equal(supportsRawMetadata, length > 0);
23+
24+
if (supportsRawMetadata)
25+
{
26+
var metadataReader = new MetadataReader(blob, length);
27+
string assemblyName = metadataReader.GetString(metadataReader.GetAssemblyDefinition().Name);
28+
Assert.Equal(assembly.GetName().Name, assemblyName);
29+
}
30+
}
31+
}
32+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<RootNamespace>System.Runtime.Loader.Tests</RootNamespace>
44
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
5+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
56
<TestRuntime>true</TestRuntime>
67
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
78
<!-- Some tests rely on no deps.json file being present. -->
@@ -16,6 +17,7 @@
1617
<ItemGroup>
1718
<Compile Include="ApplyUpdateTest.cs" />
1819
<Compile Include="ApplyUpdateUtil.cs" />
20+
<Compile Include="AssemblyExtensionsTest.cs" />
1921
<Compile Include="AssemblyLoadContextTest.cs" />
2022
<Compile Include="CollectibleAssemblyLoadContextTest.cs" />
2123
<Compile Include="ContextualReflection.cs" />

src/mono/System.Private.CoreLib/src/System/Reflection/Metadata/AssemblyExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ namespace System.Reflection.Metadata
88
public static class AssemblyExtensions
99
{
1010
[CLSCompliant(false)]
11-
public static unsafe bool TryGetRawMetadata(this Assembly assembly, out byte* blob, out int length) => throw new NotImplementedException();
11+
public static unsafe bool TryGetRawMetadata(this Assembly assembly, out byte* blob, out int length)
12+
{
13+
ArgumentNullException.ThrowIfNull(assembly);
14+
15+
blob = null;
16+
length = 0;
17+
return false;
18+
}
1219
}
1320
}

0 commit comments

Comments
 (0)