How can the value referenced by a variable of type float array have type byte array?

StackOverflow https://stackoverflow.com/questions/23331340

  •  10-07-2023
  •  | 
  •  

문제

The NAudio library provides the following interface.

interface ISampleProvider
{
    WaveFormat WaveFormat { get; }
    int Read(float[] buffer, int offset, int count);
}

I made a class that implements this interface. In my implementation of Read I am getting very strange behavior. The type of the buffer argument is showing up as byte[] even though that variable is declared to be a float[].

    int Read(float[] buffer, int offset, int count)
    {
        //The type of buffer is System.Byte[]!
        var type = buffer.GetType();
        ...
    }

How can this be?

I don't know how the NAudio library is calling my Read implementation, but I'm guessing it may be via some unsafe interop.

도움이 되었습니까?

해결책

You're seeing the WaveBuffer class in action, which is a way that NAudio tricks .NET into letting us "cast" a byte[] into a float[] without having to copy data or pin buffers.

Read more about it:

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top