Skip to content
Draft
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
85086ea
Update core library to add support for generics <T> (#242)
josesimoes Apr 18, 2025
2bb1d2d
Work CI-CD
josesimoes Apr 21, 2025
f9a9158
Work CI-CD
josesimoes Apr 21, 2025
e37e4b8
Work CI-CD
josesimoes Apr 21, 2025
488ab3b
Merge branch 'main' of https://github.com/nanoframework/CoreLibrary i…
josesimoes Apr 29, 2025
6303710
Work CI-CD
josesimoes Apr 30, 2025
7005c52
Fix and improvements in `String` class (#248)
josesimoes Sep 4, 2025
fa61139
Merge branch 'main' into develop
josesimoes Sep 9, 2025
ed90c8e
Add `Span<T>` and `ReadOnlySpan<T>` (#249)
josesimoes Sep 11, 2025
32a57c5
Add `ByRefLikeGenerics` to runtime features (#250)
josesimoes Sep 18, 2025
ab7909f
Remove exclusion of `DebuggerTypeProxyAttribute` (#251)
josesimoes Sep 18, 2025
e99e3aa
Add `IsReferenceOrContainsReferences` runtime helper (#252)
josesimoes Sep 24, 2025
03e6ddb
Work CD-CI
josesimoes Sep 24, 2025
412684a
Add collection related interfaces and classes (#253)
josesimoes Sep 24, 2025
3cf3175
Add IntelliSense comments to `Nullable` classes (#254)
josesimoes Sep 24, 2025
5d24cd3
Remove test struct with nested field ref
josesimoes Sep 24, 2025
d2b908e
Work CI-CD
josesimoes Sep 25, 2025
668efa7
Bump test framework sub-module @602d512
josesimoes Sep 27, 2025
7c0182d
Merge branch 'main' of https://github.com/nanoframework/CoreLibrary i…
josesimoes Nov 6, 2025
5b23ee9
Add `SZArrayHelper` and `Unsafe.As<T>` (#257)
josesimoes Nov 6, 2025
4738eed
Fixed null references and added == and != overloads in `Guid` (#247)
dcyonce Nov 24, 2025
51be470
Add benchmark tests for ToString
josesimoes Nov 24, 2025
ce5c6d8
Fix `Guid` constructor (#259)
josesimoes Nov 24, 2025
d92a1c1
Update test framework sub-module @9f6e02c
josesimoes Nov 27, 2025
903c2b2
Work CI-CD
josesimoes Nov 27, 2025
b0bf1e3
Work CI-CD
josesimoes Dec 4, 2025
2a79b7a
Work CI-CD
josesimoes Dec 5, 2025
7977eda
Add new constructors to `Span<T>` and `ReadOnlySpan<T>` (#260)
josesimoes Dec 5, 2025
6996df8
Moved `String.Format()` to native (#261)
josesimoes Dec 6, 2025
b3427e4
Improvements in `ReadOnlySpan<T>` and `Span<T>` (#262)
josesimoes Dec 12, 2025
f4163d3
Work CI-CD
josesimoes Jan 6, 2026
0d014fa
Add `MemoryExtensions` class (#263)
josesimoes Jan 6, 2026
ebb5481
Add interfaces for generic collections (#264)
josesimoes Jan 8, 2026
bfcba6a
Update test sub-module @2e5c15c
josesimoes Jan 14, 2026
6c93600
Add implicit operator to `Span<T>` to allow conversion from array (#265)
josesimoes Jan 14, 2026
50352eb
Work CI-CD
josesimoes Jan 14, 2026
7c5e825
Add .github/copilot-instructions.md for Copilot cloud agent onboardin…
Copilot Apr 8, 2026
46cf909
Merge branch 'main' into develop
josesimoes Apr 8, 2026
c326223
Merge branch 'main' of https://github.com/nanoframework/CoreLibrary i…
josesimoes Apr 16, 2026
2aa5d2a
Fix unit test for `String.Format()` (#269)
josesimoes Apr 23, 2026
1474d8c
Improvements in `String` class (#270)
josesimoes Apr 24, 2026
1d0f540
Fix file
josesimoes Apr 27, 2026
779da80
Rewrote `Guid.CompareTo` (#271)
josesimoes Apr 27, 2026
30df403
Fix `ReadOnlySpan<T>` operator == to use identity semantics (#272)
josesimoes Apr 27, 2026
17440c8
Add unit tests group to release notes config
josesimoes Apr 27, 2026
7a33d32
Update test framework sub-module @521b03b
josesimoes May 6, 2026
974d1db
Work CI-CD
josesimoes May 7, 2026
eac1915
Update test framework sub-module @6aa00b2
josesimoes May 7, 2026
9cba9c3
Update test framework sub-module @9d8b809
josesimoes May 7, 2026
6cf6098
Merge branch 'main' of https://github.com/nanoframework/CoreLibrary i…
josesimoes May 8, 2026
3dd272a
Merge branch 'main' of https://github.com/nanoframework/CoreLibrary i…
josesimoes May 11, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Copilot Instructions for nanoFramework CoreLibrary

## Repository Overview

This repository contains the **Base Class Library (BCL)** for [.NET nanoFramework](https://www.nanoframework.net/) — a free, open-source platform that enables writing managed .NET code for embedded systems and microcontrollers. The BCL provides the core `System` namespace types and is the nanoFramework equivalent of `mscorlib`.

## Key Architectural Concepts

### This Is NOT Standard .NET
- The code targets embedded systems (MCUs) with severe memory and flash constraints.
- Many methods are implemented in native C++ ("native stubs") and are declared with `[MethodImpl(MethodImplOptions.InternalCall)]` — **do not add a body to these methods**.
- Some standard .NET APIs are intentionally unsupported (throw `NotSupportedException`) to preserve assembly/image size. Do not remove these stubs; they exist to satisfy interface contracts.
- The library targets `TargetFrameworkVersion v1.0` (the nanoFramework target, not desktop .NET).

### Two Library Flavours
| Project | Description | NuGet |
|---|---|---|
| `nanoFramework.CoreLibrary` | Full BCL **with** `System.Reflection` | `nanoFramework.CoreLibrary` |
| `nanoFramework.CoreLibrary.NoReflection` | BCL **without** reflection (smaller flash footprint) | `nanoFramework.CoreLibrary.NoReflection` |

Both produce an assembly named `mscorlib`. The no-reflection variant excludes files under `System/Reflection/` and sets no `NANOCLR_REFLECTION` define.

### Project File Format
Projects use `.nfproj` files (nanoFramework MSBuild project system), not standard `.csproj`. These require Visual Studio with the **nanoFramework VS extension** installed, or MSBuild with `NFProjectSystem.CSharp.targets`.

## Repository Structure

```
nanoFramework.CoreLibrary/ # Main BCL project (with reflection)
System/ # All System.* source files
Collections/ # IEnumerable, ArrayList, generic interfaces
Diagnostics/ # Debug, Debugger, attributes
Globalization/ # CultureInfo, DateTimeFormatInfo, etc.
IO/ # IOException
Reflection/ # Assembly, MethodInfo, FieldInfo, etc.
Runtime/ # CompilerServices, InteropServices, Remoting
Threading/ # Thread, Monitor, Timer, WaitHandle, etc.
CoreLibrary.nfproj
Directory.Build.props

nanoFramework.CoreLibrary.NoReflection/ # BCL without reflection
System/ # Subset of System.* files (no Reflection/)
CoreLibrary.NoReflection.nfproj

Tests/ # Unit tests (one folder per test suite)
NFUnitTestArithmetic/
NFUnitTestArray/
NFUnitTestBitConverter/
NFUnitTestSystemLib/ # Covers most primitive types, strings, DateTime, etc.
NFUnitTestThread/
... (21 test suites total)

nanoFramework.TestFramework/ # Git submodule: test framework source
azure-pipelines.yml # CI/CD pipeline (Azure Pipelines, Windows)
nanoFramework.CoreLibrary.sln # Main solution (library + tests)
nanoFramework.CoreLibrary.Benchmarks.sln # Benchmarks solution
version.json # Nerdbank.GitVersioning configuration
```
Comment thread
josesimoes marked this conversation as resolved.

## Build & Test

### Build Requirements
- **Windows only** — the nanoFramework build toolchain runs on Windows (Azure Pipelines uses `windows-latest`).
- Visual Studio 2022 with the nanoFramework VS extension, or MSBuild 17+.
- NuGet packages are restored automatically; `packages.lock.json` enforces locked restore in CI.

### Building
```powershell
# Restore NuGet packages first
nuget restore nanoFramework.CoreLibrary.sln

# Build the solution (Release)
msbuild nanoFramework.CoreLibrary.sln /p:Configuration=Release /p:Platform="Any CPU"

# Build only CoreLibrary (Debug)
msbuild nanoFramework.CoreLibrary\CoreLibrary.nfproj /p:Configuration=Debug
```

### Running Tests
Tests use the [nanoFramework.TestFramework](https://github.com/nanoframework/nanoFramework.TestFramework) with the **nanoCLR Win32 emulator** (no real hardware needed by default).

```powershell
# From Developer Command Prompt for VS 2022:
vstest.console.exe .\Tests\NFUnitTestBitConverter\bin\Release\NFUnitTest.dll ^
/Settings:.\.runsettings ^
/TestAdapterPath:.\nanoFramework.TestFramework\source\TestAdapter\bin\Debug\net4.8

# Run all tests using runsettings:
# .runsettings -> uses preview nanoCLR from NuGet (default)
# local_clr.runsettings -> uses a locally built nanoCLR
```

Key runsettings config:
- `IsRealHardware=False` — runs on emulator
- `UsePreviewClr=True` — downloads latest preview nanoCLR
- `MaxCpuCount=1` — tests run sequentially
- `TestSessionTimeout=1200000` (20 min)

### Adding Tests
- Each test suite is a separate `.nfproj` in `Tests/`.
- Use `nanoFramework.TestFramework` attributes: `[TestClass]`, `[TestMethod]`, `[Setup]`, `[Cleanup]`, `[DataRow]`.
- **Do not** reference the NuGet `nanoFramework.TestFramework` — use the project reference from `nanoFramework.TestFramework/` submodule instead (this is required for the CoreLibrary because it replaces mscorlib).
- **Do not** reference `mscorlib`, `nanoFramework.TestFramework`, or `nanoFramework.UnitTestLauncher` NuGet packages; use project references.
- Every new API **must** have test coverage for all methods, properties, events, and exceptions.

## Coding Conventions

### File Header
Every `.cs` file must begin with:
```csharp
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
```

### Naming
| Item | Convention | Example |
|---|---|---|
| Constants | PascalCase | `MaxValue` |
| Private/internal fields | `_camelCase` | `_thread` |
| Private/internal static fields | `s_camelCase` | `s_instance` |
| Public members | PascalCase | `GetEnumerator` |

### Formatting (from `.editorconfig`)
- Indent: 4 spaces (C#), 2 spaces (XML/project files)
- Line endings: CRLF
- Encoding: UTF-8 BOM
- Always use braces, even for single-line blocks
- `using` directives outside namespace
- System directives first

### C# Language
- Language version: C# 13.0 (main project); `default` for NoReflection variant
- Avoid `var` — use explicit types
- Prefer expression-bodied members where appropriate
- `[MethodImpl(MethodImplOptions.InternalCall)]` for native implementations — never add a body

### Assembly Size Awareness
Comment thread
josesimoes marked this conversation as resolved.
- Avoid adding features that significantly increase binary size.
- Throw `NotSupportedException` (without a message string) when a method cannot be implemented on the constrained target.
- Comment rationale when omitting functionality for size reasons.

## CI/CD Pipeline

CI runs on **Azure Pipelines** (`azure-pipelines.yml`), not GitHub Actions. There is no GitHub Actions CI workflow for the main build. The pipeline:
1. Builds `nanoFramework.CoreLibrary.sln` on Windows
2. Runs all unit tests with nanoCLR emulator
3. Packages and publishes two NuGet packages
4. Updates dependent repositories on tag releases

## Important Notes for Contributions

1. **Avoid breaking native interop**: Methods marked `[MethodImpl(MethodImplOptions.InternalCall)]` have matching native C++ implementations in the nanoFramework interpreter. Changing their signatures requires coordinated changes to the native runtime.
2. **Two-project sync**: When adding or removing files from `nanoFramework.CoreLibrary`, evaluate whether the same change applies to `nanoFramework.CoreLibrary.NoReflection`. Reflection-related files belong only in the main project.
3. **Versioning**: Versions are managed by [Nerdbank.GitVersioning](https://github.com/dotnet/Nerdbank.GitVersioning) via `version.json`. Do not manually edit version numbers.
4. **Assembly signing**: The assembly is strong-named with `key.snk`. Do not replace or remove this file.
5. **`Friends.cs`**: This file declares `InternalsVisibleTo` attributes for test projects. Add new test projects here if they need access to internals.

## Known Constraints & Workarounds

- The build requires Windows and the nanoFramework MSBuild extension. Running builds in a Linux environment (e.g., standard GitHub Actions) will fail because `NFProjectSystem.CSharp.targets` is not available there.
- Tests must be run via `vstest.console.exe` with the nanoFramework test adapter; standard `dotnet test` does not work.
- The `RestoreLockedMode` is enabled in CI to prevent unexpected package version changes — if packages need updating, the `packages.lock.json` must be regenerated and committed.
- Some APIs intentionally throw `NotSupportedException` or return stub values — this is by design for embedded constraints, not a bug.
5 changes: 3 additions & 2 deletions .runsettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
Expand All @@ -12,5 +12,6 @@
<Logging>Verbose</Logging>
<IsRealHardware>False</IsRealHardware>
<RunnerExtraArguments> --forcegc </RunnerExtraArguments>
<UsePreviewClr>True</UsePreviewClr>
</nanoFrameworkAdapter>
</RunSettings>
</RunSettings>
36 changes: 31 additions & 5 deletions Tests/NFUnitTestGC/TestGC.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.


using System;
using nanoFramework.TestFramework;

namespace NFUnitTestGC
Expand All @@ -15,16 +16,14 @@ public void TestGCStress()
int maxArraySize = 1024 * 32;
object[] arrays = new object[600];

// Starting TestGCStress

for (int loop = 0; loop < 100; loop++)
{
OutputHelper.WriteLine($"Running iteration {loop}");

for (int i = 0; i < arrays.Length - 1;)
{
OutputHelper.WriteLine($"Alloc array of {maxArraySize} bytes @ pos {i}");
arrays[i++] = new byte[maxArraySize]; ;
arrays[i++] = new byte[maxArraySize];

OutputHelper.WriteLine($"Alloc array of 64 bytes @ pos {i}");
arrays[i++] = new byte[64];
Expand All @@ -37,8 +36,35 @@ public void TestGCStress()
arrays[i] = null;
}
}
}

[TestMethod]
public void TestGetTotalMemory()
{
// create several objects
object[] objects = new object[100];

for (int i = 0; i < objects.Length; i++)
{
objects[i] = new object();
}

// get total memory
long totalMemory = GC.GetTotalMemory(false);
OutputHelper.WriteLine($"Total memory: {totalMemory} bytes");

// release objects
for (int i = 0; i < objects.Length; i++)
{
objects[i] = null;
}

// get total memory, forcing full collection
long totalMemoryAfterCollection = GC.GetTotalMemory(true);
OutputHelper.WriteLine($"Total memory: {totalMemoryAfterCollection} bytes");

// Completed TestGCStress
// check if memory was released
Assert.IsTrue(totalMemory > totalMemoryAfterCollection, "Memory was not released");
}
}
}
4 changes: 4 additions & 0 deletions Tests/NFUnitTestSystemLib/NFUnitTestSystemLib.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
<IsTestProject>true</IsTestProject>
<TestProjectType>UnitTest</TestProjectType>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<LangVersion>13.0</LangVersion>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="RuntimeHelpersTests.cs" />
<Compile Include="UnitTestMemoryExtensions.cs" />
<Compile Include="UnitTestNullable.cs" />
<Compile Include="UnitTestUInt64.cs" />
<Compile Include="UnitTestUInt32.cs" />
<Compile Include="UnitTestSingle.cs" />
Expand Down
68 changes: 68 additions & 0 deletions Tests/NFUnitTestSystemLib/RuntimeHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using nanoFramework.TestFramework;

namespace NFUnitTestSystemLib
{
[TestClass]
class RuntimeHelpersTests
{
[TestMethod]
public static void IsReferenceOrContainsReferences()
{
Assert.IsFalse(RuntimeHelpers.IsReferenceOrContainsReferences<int>());
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<string>());
Assert.IsFalse(RuntimeHelpers.IsReferenceOrContainsReferences<Guid>());
Assert.IsFalse(RuntimeHelpers.IsReferenceOrContainsReferences<StructWithoutReferences>());
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<StructWithReferences>());
Assert.IsFalse(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithoutReferences>());
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithReferences>());
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<Span<char>>());
Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<ReadOnlySpan<char>>());
//Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithRef>());
//Assert.IsTrue(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithNestedRef>());
}

private struct StructWithoutReferences
{
public int a, b, c;
}

private struct StructWithReferences
{
public int a, b, c;
public object d;
}

private ref struct RefStructWithoutReferences
{
public int a;
public long b;
}

private ref struct RefStructWithReferences
{
public int a;
public object b;
}

// TODO: add after checking viability of ref fields in ref structs
//private ref struct RefStructWithRef
//{
// public ref int a;

// internal RefStructWithRef(ref int aVal)
// {
// a = ref aVal;
// }
//}

//private ref struct RefStructWithNestedRef
//{
// public Span<char> a;
//}
}
}
Loading