microsoft / microsoft/CsWin32

Improve .NET Framework `Span<T>`/`ReadOnlySpan<T>` generation

Open
#1,651 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
2.5k
Forks
124
Avg merge
1d 3h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.

I get that .NET Framework is reaching a decade old in age, however the projects I work on require that I target a built-in runtime. Further to this, my main project is used in Windows PowerShell/PowerShell 7, so considering options like NativeAOT is not possible. What I'm finding is that setups like the below don't work on .NET Framework and are appropriately unavailable for me:

internal unsafe readonly ReadOnlySpan<winmdroot.System.Diagnostics.Debug.IMAGE_DATA_DIRECTORY> AsReadOnlySpan() => MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _0), SpanLength);

Describe the solution you'd like

Something like the following should achieve the same result and would work equally on .NET Framework with System.Memory, or .NET 8/9/10:

internal unsafe readonly ReadOnlySpan<winmdroot.System.Diagnostics.Debug.IMAGE_DATA_DIRECTORY> AsReadOnlySpan() => new(Unsafe.AsPointer(ref _0), SpanLength);

I am yet to test this within my own code as an extension method, but will do so soon.

Describe alternatives you've considered

My own extension method as mentioned above, however it'd be good to have this as a first class available option.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the generated ReadOnlySpan method shown in the issue for a .NET Framework target, then trace the source-generator entry point that emits this method. Compare the generated output with the proposed System.Memory-compatible form and verify the result on .NET Framework and modern .NET targets; done means both targets generate usable spans.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.