1

I am a protobuf-net beginner and have just installed v2r470, to be used in my C#-WinForm application. I get an "InvalidOperationException was unhandled" - "No serializer defined for type: System.Object" when serializing an ArrayList.

Serializable class:

[ProtoContract]
public class ProtoData
{
    [ProtoMember(1)]
    ArrayList list = new ArrayList();

    public ProtoData()
    {
        list.Clear();
        list.Add("Hello");
        list.Add("World");
    }
}

Serialization:

        ProtoData pData = new ProtoData();
        var file = File.Create("protodata.bin");
        Serializer.Serialize<ProtoData>(file, pData);   // <<----- error

What's missing here?

1 Answer 1

1

In most cases, protobuf-net will prefer homogeneous data; in your case you seem to be storing lists; it you make list a List<string>, then it will just work.

protobuf (the format, not protobuf-net specifically) is engineered around an expected and known schema; you can't just say "an object" - it wants to know more than that. I protobuf-net I have added various tweaks and trips to make that achievable, but the most appropriate approach would depend on what exactly your actual model is trying to do. So far, it is trying to store 2 strings; List<string> is the most appropriate way to do that. Like I say, though, there are a few other ways to do similar things with heterogeneous data.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your prompt reply. I have nested Arraylists, i.e. ArrayList containing objects with other ArrayLists etc. Serializing using BinaryFormatter works just fine, and I hoped it would in protobuf-net as well. I will look into the workload to go from ArrayList to List<>
I should note that the RuntimeTypeModel does allow you to explicitly state the item-type for lists, allowing it to work with ArrayList, but simpy using List-of-T is easier and clearer
@StefanS note; my comment above; if the objects are of the same type, it can work with ArrayList. Is it an ArrayList of ArrayList? or is it an ArrayList of some type that has (encapsulates) an ArrayList? (this matters, too)
I want to serialize an object containing several ArrayLists as well as simple types (int, string etc.). The encapsulated ArrayLists contains objects of different types, each of which also contains ArrayLists and simple types.
@StefanS "The encapsulated ArrayLists contain objects of different types" - there are ways to do that, but it would require structural changes to the model. protobuf-net is a versatile tool, but it is not a direct 1:1 swap-out from BinaryFormatter. By the very nature of storing less metadata, it needs a way to understand the model more clearly up-front. How much change is acceptable?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.