RestSharp似乎没有注意我用以下方法装饰我的一个类名的“SerializeAs”属性:
人
[Serializable, SerializeAs(Name = "person")]
public class Person
{
[SerializeAs(Name = "first-name")]
public string FirstName { get; set; }
[SerializeAs(Name = "contact-data")]
public ContactData ContactData { get; set; }
}
联系数据
public class ContactData
{
[SerializeAs(Name = "email-addresses")]
public List<EmailAddress> EmailAddresses { get; set; }
}
EmailAddress的
[SerializeAs(Name = "email-address")]
public class EmailAddress
{
[SerializeAs(Name = "address")]
public string Address { get; set; }
[SerializeAs(Name = "location")]
public string Location { get; set; }
}
我正在使用以下代码来序列化XML:
var request = new RestRequest("people/{id}.xml", Method.PUT);
request.AddParameter("id", person.Id, ParameterType.UrlSegment);
request.XmlSerializer = new XmlSerializer();
request.AddBody(person);
但是,生成的XML如下所示:
<person>
<first-name>Scott</first-name>
<contact-data>
<email-adresses>
<EmailAddress>
<address>my@email.com</address>
<location>Work</location>
</EmailAddress>
</email-adresses>
</contact-data>
</person>
您可以看到<EmailAddress>
元素似乎忽略了SerializeAs
属性,并且未按照我预期的那样序列化为“电子邮件地址”,而其他所有元素都工作得很好。有谁知道为什么会发生这种情况或如何解决它?