Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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# caliburn.micro在实现PropertyChangedBase时出现序列化问题_C#_Json_Wpf_Serialization_Caliburn.micro - Fatal编程技术网

C# caliburn.micro在实现PropertyChangedBase时出现序列化问题

C# caliburn.micro在实现PropertyChangedBase时出现序列化问题,c#,json,wpf,serialization,caliburn.micro,C#,Json,Wpf,Serialization,Caliburn.micro,我正在开发一个客户机/服务器数据驱动的应用程序,前端使用caliburn.micro,后端使用Asp.net WebApi 2 public class Person { public int Id {get;set;} public string FirstName{get;set;} ... } 该应用程序包含一个名为“Person”的类。一个“Person”对象被序列化(JSON),并使用简单的REST协议在客户端和服务器之间来回移动。该解决方案运行良好,没有任何

我正在开发一个客户机/服务器数据驱动的应用程序,前端使用caliburn.micro,后端使用Asp.net WebApi 2

public class Person
{
    public int Id {get;set;}
    public string FirstName{get;set;}
    ...
}
该应用程序包含一个名为“Person”的类。一个“Person”对象被序列化(JSON),并使用简单的REST协议在客户端和服务器之间来回移动。该解决方案运行良好,没有任何问题

问题:

为了实现NotifyOfPropertyChanged(),我为“Person”设置了父类“PropertyChangedBase”

但这次类“Person”的属性在接收端有空值

我想序列化/反序列化有问题。 这仅在实现PropertyChangedBase时发生

有人能帮我解决这个问题吗?

您需要将属性添加到您的
Person
类中,并将属性添加到您希望序列化的每个属性和字段中:

[DataContract]
public class Person : PropertyChangedBase
{
    [DataMember]
    public int Id { get; set; }

    private string _firstName;

    [DataMember]
    public string FirstName { get; set; }
}
您需要这样做,因为基类具有
[DataContract]
属性:

namespace Caliburn.Micro {
    [DataContract]
    public class PropertyChangedBase : INotifyPropertyChangedEx
    {
    }
}
但为什么这是必要的呢?理论上,应用于基类的类的存在不应影响派生的
Person
类,因为:

但是,使用的默认实例和Json.NET不尊重
DataContractAttribute
Inherited=false
属性,如下所述

[Json.NET]检测基类上的DataContractAttribute,并假设选择性加入序列化

(如需确认,请参阅确认Json.NET的此行为是否继续符合预期。)

因此,您毕竟需要添加这些属性

或者,如果您不想在所有派生类上应用数据协定属性,可以按照此处的说明切换到
DataContractJsonSerializer

如果愿意,可以将JsonMediaTypeFormatter类配置为使用DataContractJsonSerializer而不是Json.NET。为此,请将UseDataContractJsonSerializer属性设置为true

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.UseDataContractJsonSerializer = true;

您使用的是哪个JSON序列化程序?我使用的是HttpClient PostAsJsonAsync。var response=等待客户端.PostAsJsonAsync(“api/person”,person);杰出的我已经添加了属性Datacontract和DataMember。它按预期工作。谢谢!!!
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum, Inherited = false, 
AllowMultiple = false)]
public sealed class DataContractAttribute : Attribute
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.UseDataContractJsonSerializer = true;