Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 927915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:55:39+00:00 2026-05-15T19:55:39+00:00

Based on the code found here: protobuf and List<object> – how to serialize /

  • 0

Based on the code found here: protobuf and List<object> – how to serialize / deserialize? I’ve created a generic “ProtoDictionary” that has a value type of ProtoObject.

Here is my code for the 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) 
    {

    }
}

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

The problem is, when I attempt to deserialize a ProtoDictionary from SQL using the following code:

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

I get the error “InvalidOperationException: Instances of abstract classes cannot be created.”

My StackTrace is as follows:

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

I’m not sure what I’m doing wrong. Any help would be GREATLY appreciated.

Thanks,
Dan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:55:39+00:00Added an answer on May 15, 2026 at 7:55 pm

    This may be simply a limitation in “v1”. I’ve added this as a test on “v2”, and it passed (I had to invent UrlStatus and TrafficEntry to get it to compile):

        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]);
        }
    

    With “v1”, maybe simply don’t make it abstract? (workaround, not fix)

    Also; there should be no need for the SerializationInfo ctor; that is not used by protobuf-net (although you can implement protobuf-net inside BinaryFormatter by providing this method and calling into Merge)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that one has some lock based code like the following where mutexes are
I've got the following code which is based off an example i found here
Based on some other code I found from this site (This Question here -
I have tried the code examples that I found here on the stackoverfllow site,
Used code I found on SO to use the COM based Acrobat Reader to
I am using a library that generates code based on IDL definitions. Its great
I am trying to get an HTML-based recursive directory listing based on code here:
I'm working on sanitizing my Html using Jeff Atwood's code found here But the
I have created a simple API for a Rails application using token-based-authentication that supports
I am implementing a simple cache using LinkedHashMap based on the instructions found here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.