Skip to content

Commit e99e3aa

Browse files
authored
Add IsReferenceOrContainsReferences runtime helper (#252)
***NO_CI***
1 parent ab7909f commit e99e3aa

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

Tests/NFUnitTestSystemLib/NFUnitTestSystemLib.nfproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
<IsTestProject>true</IsTestProject>
2222
<TestProjectType>UnitTest</TestProjectType>
2323
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
24+
<LangVersion>13.0</LangVersion>
2425
</PropertyGroup>
2526
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2627
<ItemGroup>
28+
<Compile Include="RuntimeHelpersTests.cs" />
2729
<Compile Include="UnitTestNullable.cs" />
2830
<Compile Include="UnitTestUInt64.cs" />
2931
<Compile Include="UnitTestUInt32.cs" />
@@ -62,4 +64,4 @@
6264
<ProjectConfigurationsDeclaredAsItems />
6365
</ProjectCapabilities>
6466
</ProjectExtensions>
65-
</Project>
67+
</Project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 System;
5+
using System.Runtime.CompilerServices;
6+
using nanoFramework.TestFramework;
7+
8+
namespace NFUnitTestSystemLib
9+
{
10+
[TestClass]
11+
class RuntimeHelpersTests
12+
{
13+
[TestMethod]
14+
public static void IsReferenceOrContainsReferences()
15+
{
16+
Assert.IsFalse(RuntimeHelpers.IsReferenceOrContainsReferences<int>());
17+
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<string>());
18+
Assert.IsFalse(RuntimeHelpers.IsReferenceOrContainsReferences<Guid>());
19+
Assert.IsFalse(RuntimeHelpers.IsReferenceOrContainsReferences<StructWithoutReferences>());
20+
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<StructWithReferences>());
21+
Assert.IsFalse(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithoutReferences>());
22+
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithReferences>());
23+
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<Span<char>>());
24+
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<ReadOnlySpan<char>>());
25+
//Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithRef>());
26+
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithNestedRef>());
27+
}
28+
29+
private struct StructWithoutReferences
30+
{
31+
public int a, b, c;
32+
}
33+
34+
private struct StructWithReferences
35+
{
36+
public int a, b, c;
37+
public object d;
38+
}
39+
40+
private ref struct RefStructWithoutReferences
41+
{
42+
public int a;
43+
public long b;
44+
}
45+
46+
private ref struct RefStructWithReferences
47+
{
48+
public int a;
49+
public object b;
50+
}
51+
52+
// TODO: add after checking viability of ref fields in ref structs
53+
//private ref struct RefStructWithRef
54+
//{
55+
// public ref int a;
56+
57+
// internal RefStructWithRef(ref int aVal)
58+
// {
59+
// a = ref aVal;
60+
// }
61+
//}
62+
63+
private ref struct RefStructWithNestedRef
64+
{
65+
public Span<char> a;
66+
}
67+
}
68+
}

nanoFramework.CoreLibrary/System/Runtime/CompilerServices/RuntimeHelpers.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
namespace System.Runtime.CompilerServices
55
{
6+
#pragma warning disable S4200 // Native methods should be wrapped
7+
68
/// <summary>
79
/// Provides a set of static methods and properties that provide support for compilers. This class cannot be inherited.
810
/// </summary>
@@ -43,5 +45,20 @@ public static extern int OffsetToStringData
4345
[MethodImpl(MethodImplOptions.InternalCall)]
4446
get;
4547
}
48+
49+
#if NANOCLR_REFLECTION
50+
51+
/// <summary>
52+
/// Returns a value that indicates whether the specified type is a reference type or a value type that contains references or by-refs.
53+
/// </summary>
54+
/// <typeparam name="T">The type.</typeparam>
55+
/// <returns><see langword="true"/> if the given type is a reference type or a value type that contains references or by-refs; otherwise, <see langword="false"/>.</returns>
56+
[MethodImpl(MethodImplOptions.InternalCall)]
57+
public static extern bool IsReferenceOrContainsReferences<T>() where T : allows ref struct;
58+
59+
#endif
60+
4661
}
62+
63+
#pragma warning restore S4200 // Native methods should be wrapped
4764
}

0 commit comments

Comments
 (0)