2010-07-16 6 views
3

여기에있는 코드를 기반으로 : protobuf and List<object> - how to serialize/deserialize? ProtoObject 값 유형을 가진 일반 "ProtoDictionary"를 만들었습니다. ProtoObject를 들어Protobuf-net : 추상 클래스의 인스턴스를 만들 수 없습니다.

public class ProtoDictionary<TKey> : Dictionary<TKey, ProtoObject> 
{ 
    public void Add(TKey key, string value) 
    { 
     base.Add(key, new ProtoObject<<string>(value)); 
    } 

    public void Add(TKey key, List<string> value) 
    { 
     base.Add(key, new ProtoObject<List<string>>(value)); 
    } 

    public void Add(TKey key, List<UrlStatus> value) 
    { 
     base.Add(key, new ProtoObject<List<UrlStatus>>(value)); 
    } 

    public void Add(TKey key, Dictionary<string, string> value) 
    { 
     base.Add(key, new ProtoObject<Dictionary<string, string>>(value)); 
    } 

    public void Add(TKey key, Dictionary<string, int> value) 
    { 
     base.Add(key, new ProtoObject<Dictionary<string, int>>(value)); 
    } 

    public void Add(TKey key, List<TrafficEntry> value) 
    { 
     base.Add(key, new ProtoObject<List<TrafficEntry>>(value)); 
    } 

    public ProtoDictionary() 
    { 
     // Do nothing 
    } 

    // NOTE: For whatever reason, this class will not correctly deserialize without this method, even though 
    // the base class, Dictionary, has the SerializableAttribute. It's protected so only the framework can access it. 
    protected ProtoDictionary(SerializationInfo info, StreamingContext context) : base(info, context) 
    { 

    } 
} 

: 여기

는 ProtoDictionary 내 코드입니다

: 나는 다음과 같은 코드를 사용하여 SQL에서 ProtoDictionary를 역 직렬화 할 때

[ProtoContract] 
[ProtoInclude(1, typeof(ProtoObject<string>))] 
[ProtoInclude(2, typeof(ProtoObject<int>))] 
[ProtoInclude(3, typeof(ProtoObject<List<string>>))] 
[ProtoInclude(4, typeof(ProtoObject<Dictionary<string, string>>))] 
[ProtoInclude(5, typeof(ProtoObject<List<TrafficEntry>>))] 
[ProtoInclude(6, typeof(ProtoObject<Dictionary<string, int>>))] 
[ProtoInclude(7, typeof(ProtoObject<bool>))] 
[ProtoInclude(8, typeof(ProtoObject<double>))] 
[ProtoInclude(9, typeof(ProtoObject<decimal>))] 
[ProtoInclude(10, typeof(ProtoObject<float>))] 
[ProtoInclude(11, typeof(ProtoObject<long>))] 
[ProtoInclude(12, typeof(ProtoObject<SerializableException>))] 
[ProtoInclude(13, typeof(ProtoObject<List<UrlStatus>>))] 
[Serializable] 
public abstract class ProtoObject 
{ 
    public static ProtoObject<T> Create<T>(T value) 
    { 
     return new ProtoObject<T>(value); 
    } 

    public object Value 
    { 
     get { return ValueImpl; } 
     set { ValueImpl = value; } 
    } 

    protected abstract object ValueImpl { get; set; } 

    protected ProtoObject() 
    { 

    } 
} 

[ProtoContract] 
[Serializable] 
public sealed class ProtoObject<T> : ProtoObject 
{ 
    public ProtoObject() 
    { 

    } 

    public ProtoObject(T value) 
    { 
     Value = value; 
    } 

    [ProtoMember(1)] 
    public new T Value { get; set; } 

    protected override object ValueImpl 
    { 
     get { return Value; } 
     set { Value = (T)value; } 
    } 

    public override string ToString() 
    { 
     return Value.ToString(); 
    } 
} 

문제이며,

public T Deserialize<T>(IDataReader reader, string columnName) 
{ 
    MemoryStream stream = new MemoryStream(); 
    byte[] buffer = new byte[256];   
    long startIndex = 0; 
    long bytesRead = reader.GetBytes(reader.GetOrdinal(columnName), startIndex, buffer, 0, buffer.Length); 

    while(bytesRead == buffer.Length) 
    { 
     stream.Write(buffer, 0, (int)bytesRead);     
     startIndex += bytesRead; 
     bytesRead = reader.GetBytes(reader.GetOrdinal(columnName), startIndex, buffer, 0, buffer.Length); 
    } 

    stream.Write(buffer, 0, (int)bytesRead); 
    stream.Seek(0, SeekOrigin.Begin); 

    return (T)Utilities.Deserialize<T>(stream); 
} 

"InvalidOperationException : 추상 클래스의 인스턴스 canno 창조되어야한다. " 다음과 같이

내 스택 트레이스는 다음과 같습니다

 
    at ctorWrapper() 
    at ProtoBuf.ObjectFactory`1.Create() in c:\protobuf-net_fixed\trunk\protobuf-net\ObjectFactory.cs:line 82 
    at ProtoBuf.Serializer`1.Deserialize[TCreation](T& instance, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 568 
    at ProtoBuf.Property.PropertyMessageString`4.DeserializeImpl(TSource source, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyMessageString.cs:line 53 
    at ProtoBuf.Property.PropertyPairString`3.DeserializeImpl(TSource source, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyPairString.cs:line 53 
    at ProtoBuf.Property.PropertyList`3.DeserializeImpl(TSource source, SerializationContext context, Boolean canSetValue) in c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyList.cs:line 64 
    at ProtoBuf.Property.PropertyList`3.Deserialize(TSource source, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyList.cs:line 52 
    at ProtoBuf.Serializer`1.Deserialize[TCreation](T& instance, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 568 
    at ProtoBuf.Serializer`1.DeserializeChecked[TCreation](T& instance, SerializationContext source) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 400 
    at ProtoBuf.SerializerSimpleProxy`1.Deserialize(TValue& value, SerializationContext source) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerProxy.cs:line 100 
    at ProtoBuf.Serializer.Deserialize[T](SerializationContext source) in c:\protobuf-net_fixed\trunk\protobuf-net\Serializer.cs:line 302 
    at ProtoBuf.Serializer.Deserialize[T](Stream source) in c:\protobuf-net_fixed\trunk\protobuf-net\Serializer.cs:line 289 
    at Demand.TestFramework.Core.Utilities.Deserialize[T](MemoryStream stream) in C:\QA\trunk\TestFramework\Core\Utilities.cs:line 312 
    at Demand.TestFramework.Core.Reports.CrawlerReport.Deserialize[T](IDataReader reader, String columnName) in C:\QA\trunk\TestFramework\Core\Reports\CrawlerReport.cs:line 145 
    at Demand.TestFramework.Core.Reports.CrawlerReport.FormatSuite(Int32 parentSuiteId, Guid runId) in C:\QA\trunk\TestFramework\Core\Reports\CrawlerReport.cs:line 70 
    at Demand.TestFramework.Core.Reports.CrawlerReport.Format(Guid runId) in C:\QA\trunk\TestFramework\Core\Reports\CrawlerReport.cs:line 150 
    at ServiceLauncher.Form1.btnStart_Click(Object sender, EventArgs e) in C:\QA\trunk\TestFramework\ServiceLauncher\Form1.cs:line 24 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
    at System.Windows.Forms.Button.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.Run(Form mainForm) 
    at ServiceLauncher.Program.Main() in C:\QA\trunk\TestFramework\ServiceLauncher\Program.cs:line 16 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

내가 잘못 뭘하는지 모르겠어요. 어떤 도움 이라든지 대단히 감사 할 것입니다.

감사합니다, 댄

답변

1

"v1"의 제한 사항 일 수 있습니다. 나는 "V2"의 테스트로 added this을했습니다, 그리고 그것을 통과 (나는 그것을 컴파일 얻을 UrlStatusTrafficEntry을 발명했다) :

public enum UrlStatus { A,B } 
    public enum TrafficEntry { A } 
    [ProtoContract] 
    public class SerializableException { } 

    [Test] 
    public void TestBasicRoundTrip() 
    { 
     var item = new ProtoDictionary<string>(); 
     item.Add("abc", "def"); 
     item.Add("ghi", new List<UrlStatus> {UrlStatus.A, UrlStatus.B}); 

     var clone = Serializer.DeepClone(item); 
     Assert.AreEqual(2, clone.Keys.Count); 
     object o = clone["abc"]; 
     Assert.AreEqual("def", clone["abc"].Value); 
     var list = (IList<UrlStatus>)clone["ghi"].Value; 
     Assert.AreEqual(2, list.Count); 
     Assert.AreEqual(UrlStatus.A, list[0]); 
     Assert.AreEqual(UrlStatus.B, list[1]); 
    } 

"V1"을 통해, 어쩌면 단순히 그것을 abstract하지 않습니다 ? (문제 해결, 수정하지 않음)

또한 마찬가지입니다. SerializationInfo ctor가 필요하지 않습니다. 내가 ProtoObject 추상적하지 않는 경우, 그래서

+0

을 (당신이 이 방법을 제공하고 Merge로 호출하여 BinaryFormatter 내부 protobuf - 그물을 구현할 수 있지만)이이 protobuf-NET에서 사용하지 않는, 내가 대신이 오류 "ProtoException를 얻을 수 : 메시지 또는 하위 메시지의 종단되지 않은 그룹 ". v2를 얼마나 가깝게 사용할 수 있습니까? 아니면 v1에서이 문제를 해결하는 것이 얼마나 힘듭니다? –

+0

@Dan 전체 데이터를 가져 오지 않았거나 실수로 일부 쓰레기를 포장했기 때문에 "Unterminated group"소리가납니다. 내가 작업 할 수있는 재현 가능한 예가 있습니까? –

+0

음 ... 나는 바보처럼 느껴진다. deserialization에 사용 된 데이터가 좋지 않습니다. 일단 그것이 고쳐지면, 모든 것이 효과가있었습니다. 추상 선언을 제거 할 필요가 없습니다. 당신의 시간을 낭비하게해서 유감입니다! 도움을 주셔서 대단히 감사합니다 !!!! 다시, 당신의 시간을 낭비해서 죄송합니다. –

0

는 ProtoObject 및 ProtoObject <T>를 분리해야하는 이유가 있나요? protobuf-net은 ObjectFactory에서 T 형의 객체 (원형 교차로)를 만드는 것처럼 보입니다. 그리고 T는 Abstract 인 ProtoObject가 될 것이라고 가정합니다.

죄송합니다. 더 도움이되지 않습니다. 지금 당장은 더 이상 파고 들지 않습니다.

관련 문제