[英]Cannot implicitly convert type “System.DateTime” to “LearnScan.LearnUser.NullableDateTime”
Trying to set a value for a create user soap call and when setting a startdate for a new user: 尝试为创建用户肥皂呼叫设置值,以及为新用户设置开始日期时:
DateTime? dt = DateTime.Now;
emptyUsr.StartDate = dt;
It returns an error of "Cannot implicitly convert type 'System.DateTime' to 'LearnScan.LearnUser.NullableDateTime'" I was under the impression that DateTime? 它返回错误“无法将类型'System.DateTime'隐式转换为'LearnScan.LearnUser.NullableDateTime'”。 sets to nullable? 设置为可空?
The StartDate
property has type LearnScan.LearnUser.NullableDateTime
which is defined as: StartDate
属性的类型为LearnScan.LearnUser.NullableDateTime
,其定义为:
public partial class NullableDateTime : object, System.ComponentModel.INotifyPropertyChanged {
internal static object DateTime;
private bool isNullField;
private System.DateTime valueField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public bool IsNull {
get {
return this.isNullField;
}
set {
this.isNullField = value;
this.RaisePropertyChanged("IsNull");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public System.DateTime Value {
get {
return this.valueField;
}
set {
this.valueField = value;
this.RaisePropertyChanged("Value");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
*Solution - It wanted values for each emptyUsr.StartDate.IsNull = false
and emptyUsr.StartDate.Value= DateTime.Now;
*解决方案-它需要每个emptyUsr.StartDate.IsNull = false
和emptyUsr.StartDate.Value= DateTime.Now;
You can create an implicit conversion from DateTime?
您可以从DateTime?
创建隐式转换DateTime?
to your NullableDateTime
class: 到您的NullableDateTime
类:
namespace LearnScan.LearnUser {
public partial class NullableDateTime
{
public static implicit operator NullableDateTime(DateTime? dt)
{
if(dt.HasValue)
{
return new NullableDateTime { IsNull = false, Value = dt.Value };
}
else
{
return new NullableDateTime { IsNull = true };
}
}
}
}
The NullableDateTime
externally-defined type (since that is not part of the regular C# set of libraries and APIs) is not the same as DateTime?
NullableDateTime
外部定义的类型(因为它不是常规C#库和API集的一部分)与DateTime?
. 。 Although its short name might trick you into thinking that, its full name LearnScan.LearnUser.NullableDateTime
tells you that it is very different from System.DateTime?
尽管它的简称可能会引起您的思考,但其全名LearnScan.LearnUser.NullableDateTime
告诉您它与System.DateTime?
有很大不同System.DateTime?
(which is the actual full name of DateTime?
in .NET). (这是.NET中DateTime?
的实际全名)。 You need to understand how NullableDateTime
was implemented and how to use it. 您需要了解如何实现NullableDateTime
以及如何使用它。 It might be a different approach to a nullable DateTime type built upon the DateTime struct .NET offers, as of the short snippet you provided in the comments. 从注释中提供的简短片段开始,对于基于.NET提供的DateTime结构构建的可为null的DateTime类型,可能是一种不同的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.