4

ここにあるコードに基づいて: protobuf and List<object> - how to serialize / deserialize? ProtoObject の値の型を持つ汎用の「ProtoDictionary」を作成しました。

ProtoDictionary のコードは次のとおりです。

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) 
    {

    }
}

ProtoObject の場合:

[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();
    }
}

問題は、次のコードを使用して SQL から ProtoDictionary を逆シリアル化しようとしたときです。

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: 抽象クラスのインスタンスを作成できません」というエラーが表示されます。

私のスタックトレースは次のとおりです。

   ctorWrapper() で
   c:\protobuf-net_fixed\trunk\protobuf-net\ObjectFactory.cs:line 82 の ProtoBuf.ObjectFactory`1.Create() で
   c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 568 の ProtoBuf.Serializer`1.Deserialize[TCreation](T& インスタンス、SerializationContext コンテキスト) で
   c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyMessageString.cs:line 53 の ProtoBuf.Property.PropertyMessageString`4.DeserializeImpl(TSource ソース、SerializationContext コンテキスト) で
   c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyPairString.cs:line 53 の ProtoBuf.Property.PropertyPairString`3.DeserializeImpl(TSource ソース、SerializationContext コンテキスト) で
   c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyList.cs:line 64 の ProtoBuf.Property.PropertyList`3.DeserializeImpl (TSource ソース、SerializationContext コンテキスト、Boolean canSetValue) で
   c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyList.cs:line 52 の ProtoBuf.Property.PropertyList`3.Deserialize(TSource ソース、SerializationContext コンテキスト) で
   c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 568 の ProtoBuf.Serializer`1.Deserialize[TCreation](T& インスタンス、SerializationContext コンテキスト) で
   c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 400 の ProtoBuf.Serializer`1.DeserializeChecked[TCreation](T& インスタンス、SerializationContext ソース)
   c:\protobuf-net_fixed\trunk\protobuf-net\SerializerProxy.cs:line 100 の ProtoBuf.SerializerSimpleProxy`1.Deserialize(TValue& 値、SerializationContext ソース) で
   c:\protobuf-net_fixed\trunk\protobuf-net\Serializer.cs:line 302 の ProtoBuf.Serializer.Deserialize[T](SerializationContext ソース)
   c:\protobuf-net_fixed\trunk\protobuf-net\Serializer.cs:line 289 の ProtoBuf.Serializer.Deserialize[T](ストリーム ソース)
   C:\QA\trunk\TestFramework\Core\Utilities.cs:line 312 の Demand.TestFramework.Core.Utilities.Deserialize[T](MemoryStream ストリーム)
   C:\QA\trunk\TestFramework\Core\Reports\CrawlerReport.cs:line 145 の Demand.TestFramework.Core.Reports.CrawlerReport.Deserialize[T](IDataReader reader, String columnName) で
   C:\QA\trunk\TestFramework\Core\Reports\CrawlerReport.cs:line 70 の Demand.TestFramework.Core.Reports.CrawlerReport.FormatSuite (Int32 parentSuiteId, Guid runId) で
   C:\QA\trunk\TestFramework\Core\Reports\CrawlerReport.cs:line 150 の Demand.TestFramework.Core.Reports.CrawlerReport.Format(Guid runId) で
   C:\QA\trunk\TestFramework\ServiceLauncher\Form1.cs:line 24 の ServiceLauncher.Form1.btnStart_Click (オブジェクト送信者、EventArgs e) で
   System.Windows.Forms.Control.OnClick (EventArgs e) で
   System.Windows.Forms.Button.OnClick (EventArgs e) で
   System.Windows.Forms.Button.OnMouseUp (MouseEventArgs mevent) で
   System.Windows.Forms.Control.WmMouseUp (Message& m、MouseButtons ボタン、Int32 クリック) で
   System.Windows.Forms.Control.WndProc (メッセージ & m) で
   System.Windows.Forms.ButtonBase.WndProc (メッセージ & m) で
   System.Windows.Forms.Button.WndProc (メッセージ & m) で
   System.Windows.Forms.Control.ControlNativeWindow.OnMessage (メッセージ & m) で
   System.Windows.Forms.Control.ControlNativeWindow.WndProc (メッセージ & m) で
   System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam) で
   System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG& メッセージ) で
   System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (IntPtr dwComponentID、Int32 理由、Int32 pvLoopData) で
   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32 理由、ApplicationContext コンテキスト) で
   System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32 理由、ApplicationContext コンテキスト) で
   System.Windows.Forms.Application.Run (フォーム mainForm) で
   C:\QA\trunk\TestFramework\ServiceLauncher\Program.cs:line 16 の ServiceLauncher.Program.Main() で
   System.AppDomain._nExecuteAssembly (RuntimeAssembly アセンブリ、文字列 [] 引数) で
   System.AppDomain.ExecuteAssembly (文字列 assemblyFile、証拠 assemblySecurity、文字列 [] 引数) で
   Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() で
   System.Threading.ThreadHelper.ThreadStart_Context (オブジェクトの状態) で
   System.Threading.ExecutionContext.Run (ExecutionContext executionContext、ContextCallback コールバック、オブジェクトの状態、ブール値の ignoreSyncCtx) で
   System.Threading.ExecutionContext.Run (ExecutionContext executionContext、ContextCallback コールバック、オブジェクトの状態) で
   System.Threading.ThreadHelper.ThreadStart() で

何が間違っているのかわかりません。どんな助けでも大歓迎です。

ありがとう、ダン

4

2 に答える 2

1

これは単に「v1」の制限かもしれません。これを「v2」のテストとして追加しましUrlStatusたが、合格しました(発明しTrafficEntryてコンパイルする必要がありました):

    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か?(回避策、修正ではありません)

また; SerializationInfoctorは必要ありません。これは protobuf-net では使用されません (ただし、このメソッドを提供して を呼び出すことで、内部に protobuf-netを実装できます) 。BinaryFormatterMerge

于 2010-07-16T09:11:08.407 に答える
0

ProtoObjectとProtoObject<T>を分離する必要がある理由はありますか?protobuf-netは、ObjectFactoryでタイプTのオブジェクトを(ラウンドアバウト方式で)作成しているようです。Tは抽象であるProtoObjectになると想定しています。

申し訳ありませんが、私はこれ以上役に立ちません。現時点では、さらに掘り下げる時間があまりありません。

于 2010-07-16T02:33:00.250 に答える