.NET Standard 是否在其支持的每个平台上标准化 HResult 值? [英] Does .NET Standard normalize HResult values across every platform it supports?

查看:12
本文介绍了.NET Standard 是否在其支持的每个平台上标准化 HResult 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的函数来创建一个随机文件.为了线程安全,它会在重试循环中创建文件,如果文件存在,则再次尝试.

I am creating a simple function that creates a random file. To be thread safe, it creates the file in a retry loop and if the file exists it tries again.

while (true)
{
    fileName = NewTempFileName(prefix, suffix, directory);

    if (File.Exists(fileName))
    {
        continue;
    }

    try
    {
        // Create the file, and close it immediately
        using (var stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
        {
            break;
        }
    }
    catch (IOException e)
    {
        // If the error was because the file exists, try again
        if ((e.HResult & 0xFFFF) == 0x00000050)
        {
            continue;
        }

        // else rethrow it
        throw;
    }
}

根据 MSDN,HResult 值来自 COM,这似乎表明它只能在 Windows 上工作,并且它特别将它们列为Win32 代码".但这是在一个面向 .NET Standard 的库中,理想情况下它应该适用于 每个 .NET Standard 平台都支持.

According to MSDN, the HResult value is derived from COM which would seem to indicate it will only work on Windows, and it specifically lists them as "Win32 codes". But this is in a library which targets .NET Standard and ideally it should work on every platform .NET Standard supports.

我想知道的是,我是否可以依靠上述使用 HResult 值的方法来实现跨平台?文档 不是清楚这一点.

What I am wondering is whether I can rely on the above approach that uses the value from HResult to be cross-platform? The documentation is not clear on this point.

如果不是,我如何确定其他平台上期望的 HResult 值?

If not, how do I determine what HResult values to expect on other platforms?

注意:有一个类似的问题 .NET定义通用 HRESULT 值?,但在 .NET Standard(以及对 .NET 的跨平台支持)存在之前就有人问过了,所以我不能为此目的依赖该答案.

NOTE: There is a similar question Does .NET define common HRESULT values?, but it was asked before .NET Standard (and cross-platform support for .NET) existed, so I cannot rely on that answer for this purpose.

目前,我们的代码库仅使用:

For now, our codebase only uses:

  1. 0x00000020 - ERROR_SHARING_VIOLATION
  2. 0x00000021 - ERROR_LOCK_VIOLATION
  3. 0x00000050 - ERROR_FILE_EXISTS

我们的目标是 .NET Standard 1.5.

We are targeting .NET Standard 1.5.

注意:虽然接受的答案确实满足我在这里提出的问题,但我有一个后续问题如何我可以跨平台可靠地移植通用 IOExceptions 吗?

NOTE: While the accepted answer does satisfy what I asked here, I have a follow-up question How do I make catching generic IOExceptions reliably portable across platforms?

推荐答案

Exception.HResult 值未跨平台标准化.

Exception.HResult values are not standardized across platforms.

对于 I/O 错误,.NET Core 将返回平台特定的错误代码作为 HResult.您的示例中文件的 HResult 属性值在 Linux 上已存在,在 Linux 上为 17,对于其他 Unix 系统可能是不同的值.

For I/O errors, .NET Core will return platform specific error code as HResult. The value of HResult property for file already exist in your example is going be 17 on Linux, and it may be different value for other Unix systems.

将 IO 错误映射到 Unix 上的异​​常的相关代码在这里:https://github.com/dotnet/corefx/blob/master/src/Common/src/Interop/Unix/Interop.IOErrors.cs

The relevant code that maps IO errors to exceptions on Unix is here: https://github.com/dotnet/corefx/blob/master/src/Common/src/Interop/Unix/Interop.IOErrors.cs

这篇关于.NET Standard 是否在其支持的每个平台上标准化 HResult 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆