今天如何才能接近 C# 中的不可空引用类型?

发布于 2024-08-20 02:45:46 字数 738 浏览 3 评论 0原文

我读过许多不可为空问题和答案。看起来接近 C# (4.0) 中不可空类型的最佳方法是 Jon Skeet 的 NonNullable 黑客攻击。

然而,C++/CLI 似乎通过支持托管引用解决了大部分问题:Foo%(而不是本机 C++ Foo&)。编译器通过向参数添加 modreq(IsImplicitlyDereferenced) 来实现此目的。尝试从 C# 调用这样的函数会导致:

'<FunctionName>' is not supported by the language

还有什么比 NonNullable 更好的吗?

有没有办法(合理地——即不使用反射)从 C# 调用 C++/CLI 方法 Foo::Method(Bar%)


[编辑] 目前似乎没有什么比 NonNullable>> 更好的了...我希望我能得到一些关于 C++/CLI 内容的评论,因为它已经有了至少是部分解决方案。

I've read many of the non-nullable questions and answers. It looks like the best way to get close to non-nullable types in C# (4.0) is Jon Skeet's NonNullable<> hack.

However, it seems that C++/CLI has solved much of the problem by supporting managed references: Foo% (instead of native C++ Foo&). The compiler makes this work by adding modreq(IsImplicitlyDereferenced) to the argument. Trying to call such a function from C# results in:

'<FunctionName>' is not supported by the language

Is there anything better then NonNullable<>?

Is there any way to (reasonably--i.e., w/o using reflection) call a C++/CLI method Foo::Method(Bar%) from C#?


[edit] It seems there is currently nothing better than NonNullable<>...I wish I would have gotten some comments on the C++/CLI stuff as it already has at least a partial solution.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2024-08-27 02:45:46

我已经遇到过几次这样的问题了……我还没有找到比斯基特的解决方案更好的方法。它解决了我遇到的所有案例,所以我必须投票。

我同意这是一个有点棘手的情况,我们必须诉诸于此......但他的修复确实解决了问题。

I've run into this a few times...I've yet to find anything better than Skeet's solution. It's solved all the cases I've come across, so I have to give it my vote.

I agree it's a bit of a hacky situation that we have to resort to that...but his fix does solve the problem.

遮了一弯 2024-08-27 02:45:46

是的,规范#: http://research.microsoft.com/en-us/ items/specsharp/

-- 编辑:我刚刚注意到你说的是 C# 4.0;我相当确定 Spec# 不支持该版本。尽管如此,还是值得回顾一下。

您也可以(我认为)稍后通过 Gendarme 中的规则进行检查,然后进行编译: http://www.mono-project.com/Gendarme(假设针对 4.0 运行)

Yep, spec#: http://research.microsoft.com/en-us/projects/specsharp/

-- Edit: I just noticed you said C# 4.0; I'm fairly sure Spec# doesn't support that version. Nevertheless, it's worth a review.

You may also be able (I think) to check at a slightly later stage then compile, via a rule in Gendarme: http://www.mono-project.com/Gendarme (assuming that runs against 4.0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文