如何在C#中接近不可为空的引用类型? [英] How can I get close to non-nullable reference types in C# today?
问题描述
我阅读了许多不可为空的问题和答案。看起来像是在C#(4.0)中接近非可空类型的最好方法是Jon Skeet的 NonNullable<> hack。
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.
但是,似乎C ++ / CLI已经解决了很多问题, : Foo%
(而不是原生C ++ Foo&
)。编译器通过向参数中添加 modreq(IsImplicitlyDereferenced)
来实现这一功能。尝试从C#调用这样的函数导致:
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
有没有什么比 NonNullable<> 好吗?
有任何方法(合理 - 即,使用反射)调用C ++ / CLI方法 Foo :: Method(Bar%)
从C#开始?
Is there any way to (reasonably--i.e., w/o using reflection) call a C++/CLI method Foo::Method(Bar%)
from C#?
[edit] 似乎目前没有比 > NonNullable<> ...我希望我会对C ++ / CLI的一些意见,因为它已经至少有一个部分解决方案。
[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.
推荐答案
我遇到过几次...我还没有找到比Skeet的解决方案更好的东西。它解决了我遇到的所有情况,所以我必须给它我的投票。
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.
这篇关于如何在C#中接近不可为空的引用类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!