问题描述
有没有办法建立在C#中的非可空类型(如日期时间或时间跨度)。
另外有没有办法(也许一个属性)执行不是空参数将不会被传递给方法和属性,而无需添加
如果(ARG1 == NULL)
{
抛出新的ArgumentNullException(这个属性为空)
}
的DateTime
和时间跨度
不是空的,因为它们是结构
真是让人不是类
上课。
至于你的第二个问题,有可以在C#中执行此操作的标准方式。为此,您可以使用PostSharp,这是一个AOP框架,或者,这是一种全新的语言(的扩展C#),它允许一些期望的行为。
Is there a way to create a non nullable type in C# (like DateTime or TimeSpan).?
Also is there a way (an attribute maybe) to enforce that not null arguments wouldn't be passed to methods and properties without adding
if(arg1 == null)
{
throw new ArgumentNullException("this attribute is null")
}
DateTime
and TimeSpan
are not-nullable since they are struct
s rather than class
es.
As for your second question, there is no standard way you can do this in C#. You can do this using PostSharp, which is an AOP framework, or with Spec#, which is a whole new language (an extension of C#) which allows for some of desired behavior.
这篇关于没有可空类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!