2012-02-21 3 views
2

위대한 Protobuf-NET을 사용하여 모델을 직렬화하려고합니다. 나는 속성을 사용할 수 없다. (객체는 컴파일 타임에 알려지지 않았다.) 그래서 TypeModel을 생성했다. 내 개체 모델은 TestDataObject 클래스로 구성되며이 클래스의 속성은 ITestDataExtension입니다. 추상 baseclass TestDataExtensionBase이 인터페이스 을 구현하고 TestDataExtension (코드의 myDataObjectExtA) 클래스는이 baseclass를 상속합니다. 내가 직렬화하지 않는 기본 클래스의 속성 다음 실행하면Protobuf-net 인터페이스와 추상 baseclass를 가진 typemodel을 생성합니다.

 System.IO.MemoryStream tmpMemoryStream = new System.IO.MemoryStream(); 
     RuntimeTypeModel model = TypeModel.Create(); 
     MetaType basetype = model.Add(typeof(TestDataObject), true); 
     MetaType interfaceType = model.Add(typeof(ITestDataExtension), true); 
     //MetaType extBaseType = interfaceType.AddSubType(100, typeof(TestDataExtensionBase)); 
     MetaType extType = interfaceType.AddSubType(200, myDataObjectExtA.GetType()); 
     model.Add(typeof(TestDataExtensionBase), true); 
     model.Add(myDataObjectA.Ext.GetType(), true); 
     model.CompileInPlace(); 
     model.Serialize(tmpMemoryStream, myDataObjectA); 
     byte[] tmpDat = tmpMemoryStream.ToArray(); 

, 나는 그들 직렬화 할 필요가 :

내 TypeModel는 다음과 같이 구성되어있다.

 MetaType extBaseType = interfaceType.AddSubType(100, typeof(TestDataExtensionBase)); 
     MetaType extType = extBaseType.AddSubType(200, myDataObjectExtA.GetType()); 

그러나 이것은 결과 : 제 생각에는
나는이 같은 TestDataExtensionBase에 대한 하위 유형을 추가해야 예기치 않은 하위 유형을 : TestDataExtension합니다. 내가 뭘 잘못하고 있는지 아는 사람이 있습니까? 어떤 도움을 주시면 감사하겠습니다.

답변

4

2 문제 :

  • 인터페이스 지원은 현재 회원 (때문에 여러 인터페이스 상속의 문제에 대한) 루트가 아닌 객체를 위해 제공되며, 이 주위에 가장 쉬운 방법은 내가 당신이 무엇을 설명 않습니다 다음 생각 인터페이스 멤버
  • 는 모델

의 하위 유형을 정의 할 필요가있는 래퍼 객체를 사용하는 것입니다 ...?

using System; 
using ProtoBuf.Meta; 

interface ITest 
{ 
    int X { get; set; } 
} 
abstract class TestBase : ITest 
{ 
    public int X { get; set; } // from interface 
    public int Y { get; set; } 
} 
class Test : TestBase 
{ 
    public int Z { get; set; } 
    public override string ToString() 
    { 
     return string.Format("{0}, {1}, {2}", X, Y, Z); 
    } 
} 
class Wrapper 
{ 
    public ITest Value { get; set; } 
} 
public class Program 
{ 
    static void Main() 
    { 
     var model = TypeModel.Create(); 
     model.Add(typeof (ITest), false).Add("X") 
       .AddSubType(10, typeof (TestBase)); 
     model.Add(typeof (TestBase), false).Add("Y") 
       .AddSubType(10, typeof (Test)); 
     model.Add(typeof (Test), false).Add("Z"); 
     model.Add(typeof (Wrapper), false).Add("Value"); 

     Wrapper obj = new Wrapper {Value = new Test() 
       {X = 123, Y = 456, Z = 789}}; 

     var clone = (Wrapper)model.DeepClone(obj); 
     Console.WriteLine(clone.Value); 
    } 
} 
+0

언제나처럼 anwser는 분명히 매우 단순합니다. 이것은 매력처럼 작동합니다. 빠른 응답을 보내 주셔서 감사합니다. – pabes

관련 문제