Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

.net - How can I get close to non-nullable reference types in C# today?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...