如果方法包含 out 参数,MustUseReturnValueAttribute 不会显示警告/异常

发布于 2025-01-10 20:43:24 字数 905 浏览 0 评论 0原文

正如标题所说,如果方法中包含带有“out”的参数,则该属性将被忽略,这是一个错误吗?有谁知道 C# 中的另一个属性在这种情况下会引发错误/警告?

(添加了 Unity 标签,因为这是在 Unity 中发生的,没有在独立项目上测试它)

如果您想知道为什么我需要这样的属性,它适用于返回值是需要处置的 IDisposable 对象的情况一旦不再使用带有“out”的参数。 例如:

    [JetBrains.Annotations.MustUseReturnValue]
    public PooledObject<List<Character>> GetCharactersOnSide(bool isLeftSide, out List<Character> characters)
    {
        PooledObject<List<Character>> pool = ListPool<Character>.Get(out characters);
        foreach (Character character in _characters)
            if (character.IsLeftSide == isLeftSide)
                characters.Add(character);

        return pool;
    }

编辑1:感谢@Ruzihm,我现在知道这个问题已经在 https: //youtrack.jetbrains.com/issue/RSRP-486462

然而,我仍在寻找解决方法,当我找到解决方法时会更新帖子。

Just as the title says, this attribute is ignored if the method contains any parameters with "out", is this a bug? Does anyone know another attribute in C# that can throw an error/warning in that case?

(added unity tag because this happens in unity, didn't test it on a standalone project)

In case you're wondering why I need an attribute like this, it's for cases where the return value is an IDisposable object that needs to be disposed once the parameter with "out" is no longer used.
E.g.:

    [JetBrains.Annotations.MustUseReturnValue]
    public PooledObject<List<Character>> GetCharactersOnSide(bool isLeftSide, out List<Character> characters)
    {
        PooledObject<List<Character>> pool = ListPool<Character>.Get(out characters);
        foreach (Character character in _characters)
            if (character.IsLeftSide == isLeftSide)
                characters.Add(character);

        return pool;
    }

Edit 1: Thanks to @Ruzihm I know now that this issue has already been reported on https://youtrack.jetbrains.com/issue/RSRP-486462.

I am however still looking for a workaround, will update the post when I find one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文