2012-01-16 4 views
2

protobuf를 사용하여 클래스 계층 구조를 직렬화하려는 데 문제가 있습니다. 추상 클래스를 상속 한 클래스가 구현하지 않은 속성은 올바른 값을 가져 오지 않습니다. 예를 들어, 나는 다음과 같은 계층 구조를 테스트 할 때 ... : NannyTutorChild의protobuf를 사용하여 추상 클래스로 계층을 직렬화하는 방법

[ProtoContract] 
[ProtoInclude(11, typeof(Child))] 
[ProtoInclude(12, typeof(Nanny))] 
public abstract class Person 
{ 
    [ProtoMember(1)] 
    public string Name { get; set; } 
} 

[ProtoContract] 
public class Child : Person 
{ 
    public Child(){ } 
} 

[ProtoContract] 
public class Nanny : Person 
{ 
    public Nanny() 
    { 
     Tutors = new List<ITutor<Person, Person>>(); 
    } 

    [ProtoMember(1)] 
    public List<ITutor<Person, Person>> Tutors { get; set; } 
} 

[ProtoContract] 
[ProtoInclude(11, typeof(NannyTutorChild))] 
public interface ITutor<out T, out U> 
    where T : Person 
    where U : Person 
{ 
    [ProtoMember(1, AsReference = true, DynamicType = true)] 
    T TutoredBy { get; } 

    [ProtoMember(2, AsReference = true, DynamicType = true)] 
    U Tutored { get; } 
} 

[ProtoContract] 
public abstract class Tutor<T, U> : ITutor<T, U> 
    where T : Person 
    where U : Person 
{ 
    [ProtoMember(1, AsReference = true, DynamicType = true)] 
    public T TutoredBy { get; set; } 

    [ProtoMember(2, AsReference = true, DynamicType = true)] 
    public U Tutored { get; set; } 
} 

[ProtoContract] 
public abstract class NannyTutor<U> : Tutor<Nanny, U> 
    where U : Person 
{ 
} 

[ProtoContract] 
public class NannyTutorChild : NannyTutor<Child> 
{ 
} 

모든 직렬화 복원 istances가 null로 설정되어, 올바르게 Serializer.DeepClone를 호출하기 전에 valorized있어 비록(). 내가 작동하도록 알아 낸 유일한 방법은 Tutor 클래스의 속성을 추상으로 표시하고 NannyTutorChild에서 구현하는 것입니다.

나는이 계층 구조로 바보 볼 수 있었다 알고 있지만, 우리의 실제 프로젝트에서 우리는 캐스트 또는 올바른 방법 :

를 사용하는 데 필요한 다른 우리 '교사'classm에서 파생 된 클래스 및 모든 레벨을 많이 가지고

아이디어가 있으십니까? 내가 뭔가 잘못하고 있는거야?

모두 감사합니다. 매트.

답변

2

이 시나리오는 현재 지원되지 않습니다. DynamicType과 상속의 조합에는 여전히 복잡성이 있습니다. 나는 그것을 보류 작업의 대기열에 전달하지만, 오늘은 거기에 있지 않습니다.

+0

감사합니다. 다음 추상 속성을 사용하여 구현할 것입니다 :) 좋은 일을 계속! –

+0

@ VollmonD 강조하기 위해, 상속과 함께 DynamicType 조합이 핵심 이슈라고 생각합니다. –

관련 문제