Web服务中的null DateTime数据类型问题 [英] the null DateTime datatype problem, in a web service
问题描述
嗨
我有一个web方法,如下所示,可以使用可空的DateTime类型作为参数。
在使用Web服务时,它期待DateTime值并且不接受null。
[WebMethod()]
public void UpdateRecord(DateTime?updatedDT,.... )
{
}
调用下面显示的函数时
UpdateRecord(null,....);
无法将null传递给DateTime参数。
请建议一个合适的程序来实现同样的目的。
Hi
I have a web method as shown below with nullable DateTime type as parameter.
While consuming the web service, it is expecting DateTime value and not accepting null.
[WebMethod()]
public void UpdateRecord(DateTime? updatedDT, ....)
{
}
When calling the function as shown below
UpdateRecord(null, ....);
not able to pass null to DateTime parameter.
Please suggest an appropriate procedure to achieve the same.
推荐答案
假设您无法修改UpdateRecord方法以支持可空类型,那么您可以:
1)使用Null Coalescing运算符将其转换为DateTime:
Assuming you can't modify the UpdateRecord method to support nullable types, then you could:
1) Use the Null Coalescing operator to convert it to a DateTime:
UpdateRecord(myNullableDateTime ?? DateTime.MinValue);
2)创建一个UpdateRecord版本,它接受可空类型并调用原始版本l版本通过null Coalescing运算符如上所述。
或者我可能误解了这一点:但是C#将非常乐意地允许这样做:
2) Create a version of UpdateRecord which does accept nullable types and calls the original version via the null Coalescing operator as above.
Or I may have misread that: but C# will allow this perfectly happily:
...
UpdateRecord(null);
}
void UpdateRecord(DateTime? dt)
{
...
}
所以我不这么认为......
So I don;t think so...
这篇关于Web服务中的null DateTime数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!