Skip to content

Provide a generic API to read from and write to a pointer #16143

@terrajobst

Description

@terrajobst

C# doesn't support pointer operations on generic types. We can, however, express them in IL. A prototype could like this:

public static class Unsafe
{
      [MethodImpl(MethodImplOptions.AggressiveInlining)]
      public static T Read<T>(void * p)
      {
          // Can't be expressed in C#, thus in IL:
          ldarg.0
          ldobj !!T
          ret
      }

      [MethodImpl(MethodImplOptions.AggressiveInlining)]
      public static void Write<T>(void * p, T value)
      {
          // Can't be expressed in C#, thus in IL:
          ldarg.0
          ldarg.1
          stobj !!T
          ret
      }
}

This would also solve issues like #16026 without any additional work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Runtime

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions