목록 MyClass
이 포함 된 클래스 인 MyClass
을 직렬화하려고합니다. XML에서 목록이 포함 된 T를 serialize합니다 <T>
MyClass
가 있어야하는 찾을 만
myClass.Name
을 기록 할
MyClass
다른 어떤있다.
MyClass
의 목록을
string
의 목록으로 적절하게 직렬화하는 다음 코드를 작성했습니다. 그러나 문자열 목록을 비 직렬화하지는 않습니다.
//List of actual object. It's what I use when I work with the object.
[XmlIgnore]
public List<TaskConfiguration> ChildTasks { get; set; }
//Used by the serializer to get the string list, and used
//by the serializer to deserialize the string list to.
[XmlArray("ChildTasks")]
public List<string> ChildTasksSurrogate
{
get
{
List<string> childTaskList = new List<string>();
if (ChildTasks != null)
childTaskList.AddRange(ChildTasks.Select(ct => ct.Name).ToList());
if (_childTasksSurrogate != null)
childTaskList.AddRange(_childTasksSurrogate);
//Clears it not to use it when it serializes.
_childTasksSurrogate = null;
return childTaskList;
}
set
{
_childTasksSurrogate = value;
}
}
[XmlIgnore]
private List<string> _childTasksSurrogate;
앞에서 설명한대로 직렬화가 작동합니다. 문제는 비 직렬화에 있습니다. deserialization 후 MyClass._childTasksSurrogate
은 null
입니다.
을 return 문 바로 앞에 접근하기에서 ** childTasksSurrogate **에 null을 할당하는 명확한 코드가 있습니다. –
어디에서? 반환 할 값을 null로 설정하지는 않지만, 문제 : 나는 deserialization이 getter를 호출하지 않고 setter 만 호출한다는 것을 당연시했습니다. List이 List로 변환 된 경우 플래그를 추가합니다. . 감사! –
Tipx
누가 당신이 반환하는 가치를 nulling했다. get 접근 자에서 ** _ childTasksSurrogate **에 null을 할당했습니다. –