C# DataContract,默认DataMember值
在反序列化过程中,是否有方法选择xml文件中没有的属性的默认值?C# DataContract,默认DataMember值,c#,wcf,datacontract,datamember,C#,Wcf,Datacontract,Datamember,在反序列化过程中,是否有方法选择xml文件中没有的属性的默认值? 如果xml文件中不存在mAge属性,我希望使用默认值18。可能吗 [DataContract] public class Person { public Person () { } [DataMember(Name = "Name")] public string mName { get; set; } [DataMember(Name = "Age")] public i
如果xml文件中不存在
mAge
属性,我希望使用默认值18。可能吗
[DataContract]
public class Person
{
public Person ()
{
}
[DataMember(Name = "Name")]
public string mName { get; set; }
[DataMember(Name = "Age")]
public int mAge { get; set; }
[DataMember(Name = "Single")]
public bool mIsSingle { get; set; }
};
编辑以放置答案
[DataContract]
public class Person
{
public Person ()
{
}
[DataMember(Name = "Name")]
public string mName { get; set; }
[DataMember(Name = "Age")]
public int? mAge { get; set; }
[DataMember(Name = "Single")]
public bool? mIsSingle { get; set; }
[System.Runtime.Serialization.OnDeserialized]
void OnDeserialized(System.Runtime.Serialization.StreamingContext c)
{
mAge = (mAge == null ? 18 : mAge); // 18 is the default value
}
}
这应该行得通
[DataContract]
public class Person
{
public Person ()
{
}
[DataMember(Name = "Name")]
public string mName { get; set; }
[DataMember(Name = "Age")]
public int mAge = 18;
[DataMember(Name = "Single")]
public bool mIsSingle { get; set; }
};
查看页面。您可以使用
当您需要修复数据上的值时,请使用OnDeserializedAttribute
已反序列化对象之后和之前的反序列化对象
返回图形
编辑:根据您的评论
对于bool或int,您可以使用
所以,如果xml文件中缺少这些年龄和单个属性,那么它们也将为null
这是我准备的快速样品
using System.Runtime.Serialization;
using System.ServiceModel;
using MySpace;
using System.ServiceModel.Channels;
using System;
namespace MySpace
{
[DataContract]
public class Person
{
public Person ()
{
}
[DataMember(Name = "Name")]
public string mName { get; set; }
[DataMember(Name = "Age")]
public int? mAge { get; set; }
[DataMember(Name = "Single")]
public bool? mIsSingle { get; set; }
[System.Runtime.Serialization.OnDeserialized]
void OnDeserialized(System.Runtime.Serialization.StreamingContext c)
{
mAge = (mAge == null ? 18 : mAge);
}
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
Person Method(Person dd);
}
public class Service : IService
{
public Person Method(Person dd)
{
return dd;
}
}
class Program
{
static void Main(string[] args)
{
string Url = "http://localhost:8000/";
Binding binding = new BasicHttpBinding();
ServiceHost host = new ServiceHost(typeof(Service));
host.AddServiceEndpoint(typeof(IService), binding, Url);
host.Open();
ChannelFactory<IService> fac = new ChannelFactory<IService>(binding);
fac.Open();
IService proxy = fac.CreateChannel(new EndpointAddress(Url));
Person d = new Person();
d.mName = "BuzBuza";
Console.WriteLine("Data before calling service " + (d.mAge == null ? " null " : d.mAge.Value.ToString()));
Console.WriteLine("Data before calling service " + (d.mIsSingle == null ? "null" : d.mIsSingle.Value.ToString()));
d = proxy.Method(d);
fac.Close();
host.Close();
Console.WriteLine("Data after calling service " + (d.mAge == null ? " null " : d.mAge.Value.ToString()));
Console.WriteLine("Data after calling service " + (d.mIsSingle == null ? "null" : d.mIsSingle.Value.ToString()));
Console.ReadLine();
}
}
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用MySpace;
使用System.ServiceModel.Channel;
使用制度;
命名空间MySpace
{
[数据合同]
公共阶层人士
{
公众人士()
{
}
[数据成员(Name=“Name”)]
公共字符串mName{get;set;}
[DataMember(Name=“Age”)]
公共int?mAge{get;set;}
[DataMember(Name=“Single”)]
公共bool?mIsSingle{get;set;}
[System.Runtime.Serialization.OnDeserialized]
void OnDeserialized(System.Runtime.Serialization.StreamingContext c)
{
法师=(法师==空?18:法师);
}
}
}
[服务合同]
公共接口设备
{
[经营合同]
人法(Person-dd);
}
公共课服务:IService
{
公众人物法(人物dd)
{
返回dd;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
字符串Url=”http://localhost:8000/";
Binding Binding=新的BasicHttpBinding();
ServiceHost主机=新的ServiceHost(类型(服务));
AddServiceEndpoint(typeof(IService)、绑定、Url);
host.Open();
ChannelFactory fac=新的ChannelFactory(绑定);
fac.Open();
IService proxy=fac.CreateChannel(新端点地址(Url));
人员d=新人员();
d、 mName=“BuzBuza”;
Console.WriteLine(“调用服务之前的数据”+(d.mAge==null?“null”:d.mAge.Value.ToString());
Console.WriteLine(“调用服务之前的数据”+(d.mIsSingle==null?“null”:d.mIsSingle.Value.ToString());
d=代理方法(d);
fac.Close();
host.Close();
Console.WriteLine(“调用服务“+(d.mAge==null?”null)后的数据:d.mAge.Value.ToString());
Console.WriteLine(“调用服务“+(d.mIsSingle==null?”null)后的数据:d.mIsSingle.Value.ToString());
Console.ReadLine();
}
}
使用
在反序列化之前设置值。因此,不需要进行检查,这可能会出错-如果mAge被序列化为0会怎么样?如果我想覆盖对象的值,我可以使用您的答案,因为当引用设置为null时,我知道xml文件中不存在属性。但是我如何知道xml文件中是否存在布尔属性或int属性呢?如果bolean的值设置为false或int设置为0。我无法区分未设置的Attribute和设置为默认c#值的Attribute。因为反序列化将尝试为给定结构中缺少的数据设置默认值,所以您唯一的选择是检查有效值,例如在我更新答案并检查age mAge==0时,然后设置值。您可以使用可空类型来绕过int和空,请检查我的最新答案。谢谢,你的答案解决了我的问题。使用可空类型和System.Runtime.Serialization.OnDeserialized函数是一个绝妙的主意。我的帖子被编辑以给出答案。不,这不起作用(只是测试了一下)。反序列化程序将覆盖默认值,即使该值在XML中不存在。当反序列化简单类型(例如,
double
)或结构时,这是正确的方法。在反序列化之前,您将可选字段设置为某些特定于应用程序的默认值(或无效值,例如,double.NaN
)。反序列化将覆盖XML文件中给定的所有值,并保留其他值不变。反序列化后,无法区分零值“set from XML”和零值“not being set”。
using System.Runtime.Serialization;
using System.ServiceModel;
using MySpace;
using System.ServiceModel.Channels;
using System;
namespace MySpace
{
[DataContract]
public class Person
{
public Person ()
{
}
[DataMember(Name = "Name")]
public string mName { get; set; }
[DataMember(Name = "Age")]
public int? mAge { get; set; }
[DataMember(Name = "Single")]
public bool? mIsSingle { get; set; }
[System.Runtime.Serialization.OnDeserialized]
void OnDeserialized(System.Runtime.Serialization.StreamingContext c)
{
mAge = (mAge == null ? 18 : mAge);
}
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
Person Method(Person dd);
}
public class Service : IService
{
public Person Method(Person dd)
{
return dd;
}
}
class Program
{
static void Main(string[] args)
{
string Url = "http://localhost:8000/";
Binding binding = new BasicHttpBinding();
ServiceHost host = new ServiceHost(typeof(Service));
host.AddServiceEndpoint(typeof(IService), binding, Url);
host.Open();
ChannelFactory<IService> fac = new ChannelFactory<IService>(binding);
fac.Open();
IService proxy = fac.CreateChannel(new EndpointAddress(Url));
Person d = new Person();
d.mName = "BuzBuza";
Console.WriteLine("Data before calling service " + (d.mAge == null ? " null " : d.mAge.Value.ToString()));
Console.WriteLine("Data before calling service " + (d.mIsSingle == null ? "null" : d.mIsSingle.Value.ToString()));
d = proxy.Method(d);
fac.Close();
host.Close();
Console.WriteLine("Data after calling service " + (d.mAge == null ? " null " : d.mAge.Value.ToString()));
Console.WriteLine("Data after calling service " + (d.mIsSingle == null ? "null" : d.mIsSingle.Value.ToString()));
Console.ReadLine();
}
}