我研究了FileStream是如何在C#和Read(...(方法中实现的,我可以看到:
n = ReadCore(_buffer, 0, _bufferSize);
...
Buffer.InternalBlockCopy(_buffer, _readPos, array, offset, n);
...
其中 Buffer.InternalBlockCopy 指向 buffer.cs 中的定义(波纹管(。BlockCopy 方法被定义为静态外部。 此方法在哪里定义?它是 .NET 的一部分?它是托管的还是本机的?
[System.Runtime.InteropServices.ComVisible(true)]
public static class Buffer
{
// Copies from one primitive array to another primitive array without
// respecting types. This calls memmove internally. The count and
// offset parameters here are in bytes. If you want to use traditional
// array element indices and counts, use Array.Copy.
[System.Security.SecuritySafeCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern void BlockCopy(Array src, int srcOffset,
Array dst, int dstOffset, int count);
..
}
好吧,extern
关键字意味着该方法是在C# 代码之外定义的,并且
[MethodImplAttribute(MethodImplOptions.InternalCall)]
表示它调用在公共语言运行时中定义的方法。
MethodImplOptions
文档后:
调用是内部调用,也就是说,它调用在公共语言运行库中实现的方法。