Я смотрю на Microsoft's How to: Create a Basic Data Contract for a Class or Structure, но это оставляет мне много вопросов.Основы DataContractAttribute
Они обеспечивают это очень упрощенный пример:
using System;
using System.Runtime.Serialization;
[DataContract]
public class Person
{
// This member is serialized.
[DataMember]
internal string FullName;
// This is serialized even though it is private.
[DataMember]
private int Age;
// This is not serialized because the DataMemberAttribute
// has not been applied.
private string MailingAddress;
// This is not serialized, but the property is.
private string telephoneNumberValue;
[DataMember]
public string TelephoneNumber
{
get { return telephoneNumberValue; }
set { telephoneNumberValue = value; }
}
}
Для моего случая, мне это нужно также включать в себя другой объект пользовательского класса называется ADUser
(пользователя Active Directory).
Я понимаю, что ADUser
должен быть отмечен DataContractAttribute
, но я не понимаю, как именно это можно сделать.
Вот класс Microsoft снова, но на этот раз с ADUser
поля добавлен:
using System;
using System.Runtime.Serialization;
[DataContract]
public class Person
{
// This member is serialized.
[DataMember]
internal string FullName;
// This is serialized even though it is private.
[DataMember]
private int Age;
// This is not serialized because the DataMemberAttribute
// has not been applied.
private string MailingAddress;
// This is not serialized, but the property is.
private string telephoneNumberValue;
[DataMember]
public string TelephoneNumber
{
get { return telephoneNumberValue; }
set { telephoneNumberValue = value; }
}
[DataMember]
public ADUser UserInfo { get; set; }
}
Я не очень понимаю, как и то, что все должно быть сделано для моего ADUser
класса, но я уверен, что private
вещи могут быть оставлены нетронутыми.
Как мне нужно исправить это ADUser
класс пример?
public class ADUser
{
private string first, last, loginID;
public ADUser() {
first = null;
last = null;
loginID = null;
}
private void getInfo() {
// code goes here
// which sets loginID;
}
public void SetName(string first, string last) {
this.first = first;
this.last = last;
getInfo();
}
public string LoginID { get { return loginID; } }
}
Вы пытались просто поместить 'DataMember' в каждое поле в 'AdUser' и пометить' ADUser' 'DataContract'? – outcoldman
как @outcoldman сказал, просто нужно сделать то же самое для ADUser. в принципе, каждый DataMember в DataContact должен быть сериализуемым, значит, если есть класс, он должен быть DataContact как weel. Кроме того, необходимо убедиться, что класс имеет конструктор, который не требует параметров –
Это так просто? ОК! :) – jp2code