简体   繁体   English

无法将类型“ System.DateTime”隐式转换为“ LearnScan.LearnUser.NullableDateTime”

[英]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 = falseemptyUsr.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.

相关问题 不能隐式转换类型System.DateTime? 到System.DateTime - cannot implicitly convert type System.DateTime? to System.DateTime 无法将类型“System.DateTime”隐式转换为 IEnumerable - Cannot implicitly convert type 'System.DateTime' to IEnumerable 无法在C#中将类型字符串隐式转换为system.datetime - cannot implicitly convert type string to system.datetime in c# 无法将类型“字符串”隐式转换为“System.DateTime” - Cannot implicitly convert type 'string' to 'System.DateTime' C#无法将类型&#39;string&#39;隐式转换为&#39;System.DateTime&#39; - C# Cannot implicitly convert type 'string' to 'System.DateTime' 错误返回时无法将类型&#39;string&#39;隐式转换为&#39;System.DateTime&#39; - Error Cannot implicitly convert type 'string' to 'System.DateTime' on return 无法将类型&#39;System.DateTime&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'System.DateTime' to 'string' 无法将布尔类型隐式转换为System.DateTime(C#) - Cannot implicitly convert type bool to System.DateTime (C#) 无法将类型 'string' 隐式转换为 'System.DateTime' - 使用 t - Cannot implicitly convert type 'string' to 'System.DateTime' - with t 无法在 C# 中将类型“字符串”隐式转换为“System.DateTime” - Cannot implicitly convert type 'string' to 'System.DateTime' in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM