c# - Protobuf-net:无法创建抽象类的实例

标签 c# protobuf-net

基于此处找到的代码: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) 
    {

    }
}

对于原型(prototype)对象:

[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:无法创建抽象类的实例。”

我的StackTrace如下:

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

我不确定我做错了什么。任何帮助将不胜感激。

谢谢, 丹

最佳答案

这可能只是“v1”中的一个限制。我有 added this作为对“v2”的测试,它通过了(我必须发明 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”,也许只是不要让它抽象? (解决方法,未修复)

还有;应该不需要 SerializationInfo 构造函数; protobuf-net 不使用它(尽管您可以通过提供此方法并调用 MergeBinaryFormatter实现 protobuf-net)

关于c# - Protobuf-net:无法创建抽象类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3261310/

相关文章:

c# - 如何在 C# 中打印 n 次 n 而无需循环、递归或 goto

c# - 如何在 Windows 窗体应用程序中构建启动画面?

c# - 检测到 Protobuf-net 可能的递归 : serialize children and parents

c# - 使用 protobuf-net 进行质量过滤

c# - 在使用长度前缀反序列化之前读取长度前缀

C# 访问子类属性

c# - 如何从 .NET 应用程序启动默认媒体播放器?

c# - 具有字典属性的 protobuf-net 类

c# - 如何更快地加载多个网格

c++ - 如何将重复的字段部分合并到消息中?