我正在阅读 .net 框架源代码: https://referencesource.microsoft.com
我发现 BCL 包含两个完全相同的类:SafeWaitHandle
和 SafeFileHandle
。他们都有完全相同的代码!
来自引用源(SafeFileHandle):
[System.Security.SecurityCritical] // auto-generated_required
public sealed class SafeFileHandle: SafeHandleZeroOrMinusOneIsInvalid {
private SafeFileHandle() : base(true)
{
}
public SafeFileHandle(IntPtr preexistingHandle, bool ownsHandle) : base(ownsHandle) {
SetHandle(preexistingHandle);
}
[System.Security.SecurityCritical]
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
override protected bool ReleaseHandle()
{
return Win32Native.CloseHandle(handle);
}
}
来自引用源(SafeWaitHandle):
[System.Security.SecurityCritical] // auto-generated_required
public sealed class SafeWaitHandle : SafeHandleZeroOrMinusOneIsInvalid
{
// Called by P/Invoke marshaler
private SafeWaitHandle() : base(true)
{
}
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public SafeWaitHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle)
{
SetHandle(existingHandle);
}
[System.Security.SecurityCritical]
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
override protected bool ReleaseHandle()
{
return Win32Native.CloseHandle(handle);
}
}
我看到构造函数对 IntPtr
参数和 SafeWaitHandle 的注释有不同的名称,试图告诉我一些事情。但我无法理解,因为代码是相同的,而且据我所知,这些类应该提供相同的行为。
有人知道为什么微软的人创建了这些 equals 类吗?为什么我应该更喜欢一门课而不是另一门课,在什么情况下?
最佳答案
区别在于语义。大多数句柄都是相似的——只是对某些资源的引用(通常只是一个指针)。但是,区分它们很有用。如果您有一些接受 SafeFileHandle
的 api - 它不会接受 SafeWaitHandle
(或存在的其他类型的句柄),这可能会防止一些细微的错误。如果它改为接受一些抽象的 Handle
- 那么有人可以在那里传递任何句柄,不代表对文件的引用。因此,不同类型的句柄由不同的类表示(即使具有相同的实现)+ C# 类型安全有利于防止将一种类型的句柄传递到需要另一种类型的句柄的地方。
关于c# - SafeWaitHandle 与 SafeFileHandle c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47252330/