'Is there a way to Marshal a byte[] into a string without copying in C#?

I am in the process of building a sub-part of a native application, namely a Unity application that runs as a child of a different application written in a completely different language. Since we have to run on mobile devices and we need to share a lot of data between the two parts, we are looking into a way to pass pointers to strings from the wrapper application to the Unity child.

I have however run into a problem when it comes to marshalling, namely that Marshal.PtrToString(Ansi/Auto/Etc) all specify in the documentation that they will copy the content of the IntPtr to a new managed string, which defeats the point.

Our goal is to read the data as a string without creating another copy. It has to be seen as a string at compile time as a library we use only accepts strings as arguments.

Is there any way to do so? Unsafe code is acceptable.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source