Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有自定义Get的DataMember属性?_C#_Json_Rest_Properties_Datacontract - Fatal编程技术网

C# 具有自定义Get的DataMember属性?

C# 具有自定义Get的DataMember属性?,c#,json,rest,properties,datacontract,C#,Json,Rest,Properties,Datacontract,并试图找出如何在没有自动属性的情况下拥有DataMember。基本上,我有一个字段是以历元格式表示的日期时间,我希望属性是日期时间,所以我尝试在属性的get中进行转换。我不知道如何准确地格式化这个 由于请求了代码,请查看以下内容: // The date looks like this in the JSON "someEpochDateTime": 1428785212000, // I thought I could work around it using the following

并试图找出如何在没有自动属性的情况下拥有DataMember。基本上,我有一个字段是以历元格式表示的日期时间,我希望属性是日期时间,所以我尝试在属性的get中进行转换。我不知道如何准确地格式化这个

由于请求了代码,请查看以下内容:

// The date looks like this in the JSON 
"someEpochDateTime": 1428785212000,

// I thought I could work around it using the following code, however
// I get a warning saying someEpochDateTime is never set.

[DataMember(Name = "someEpochDateTime")]
private long someEpochDateTime;

public DateTime test
{
get { return DateTimeConverter.FromUnixTime(someEpochDateTime); }
}

像这样使用可以创建datereturn属性,该属性将返回日期

     [DataContract]
     public class Mycontractclass
     {     
         // Apply the DataMemberAttribute to the property.
        [DataMember]
        public DateTime datereturn
        {
           get
           {
              return this.dateCreated.HasValue
                 ? this.dateCreated.Value
                 : DateTime.Now;
           }

           set { this.dateCreated = value; }
        }

        private DateTime? dateCreated = null;
    }

显然,我的上一次编辑实际上是一个解决方案,我只是出于某种原因得到了一个编译器警告

[DataMember(Name = "someEpochDateTime")]
private long someEpochDateTime;

public DateTime test
{
    get { return DateTimeConverter.FromUnixTime(someEpochDateTime); }
}

以下是如何进行转换:。请在
DateTime
getter和setter中调用此代码显示当前代码。我已编辑了您的标题。请参阅“”,其中一致意见是“不,他们不应该”。更新以添加一些代码。是的,但作为DataContract中的DataMember,我正试图弄清楚如何做到这一点。@user1970794将属性添加到类和属性中,请参阅编辑