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

  • SEARCH
  • Home
  • 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 6197557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:50:32+00:00 2026-05-24T03:50:32+00:00

I am using version 2 of ProtoBuf-net, and currently I’m geting the error Unable

  • 0

I am using version 2 of ProtoBuf-net, and currently I’m geting the error “Unable to determine member: A”

Is it possible to create a run-time model for Protobuf-net when we use ClassOfType<T>? If so, can anyone spot what I’m missing in the below code?

btw: this request is modelled off of Deserialize unknown type with protobuf-net I could get a version of this going just fine… but they are using an abstract base class, not a generic class of T.

THIS IS A WORKING EXAMPLE (stuff that wasn’t working is removed).

using System;
using System.IO;
using NUnit.Framework;
using ProtoBuf;
using ProtoBuf.Meta;

namespace ProtoBufTestA2
{
    [TestFixture]
    public class Tester
    {
        [Test]
        public void TestMsgBaseCreateModel()
        {
            var BM_SD = new Container<SomeDerived>();

            using (var o = BM_SD) {
                o.prop1 = 42;
                o.payload = new SomeDerived();
                using (var d = o.payload) {
                    d.SomeBaseProp = -42;
                    d.SomeDerivedProp = 62;
                }
            }

            var BM_SB = new Container<SomeBase>();
            using (var o = BM_SB) {
                o.prop1 = 42;
                o.payload = new SomeBase();
                using (var d = o.payload) {
                    d.SomeBaseProp = 84;
                }
            }
            var model = TypeModel.Create();

            model.Add(typeof(Container<SomeDerived>), true);  // BM_SD
            model.Add(typeof(Container<SomeBase>), true);  // BM_SB
            model.Add(typeof(SomeBase), true); // SB
            model.Add(typeof(SomeDerived), true);  // SD
            model[typeof(SomeBase)].AddSubType(50, typeof(SomeDerived)); // SD

            var ms = new MemoryStream();

            model.SerializeWithLengthPrefix(ms, BM_SD, BM_SD.GetType(), ProtoBuf.PrefixStyle.Base128, 0);

            model.SerializeWithLengthPrefix(ms, BM_SB, BM_SB.GetType(), ProtoBuf.PrefixStyle.Base128, 0);
            ms.Position = 0;
            var o1 = (Container<SomeDerived>)model.DeserializeWithLengthPrefix(
                ms
                , null
                , typeof(Container<SomeDerived>), PrefixStyle.Base128, 0);
            var o2 = (Container<SomeBase>)model.DeserializeWithLengthPrefix(
                ms
                , null
                , typeof(Container<SomeBase>), PrefixStyle.Base128, 0);
        }
    }

    [ProtoContract]
    public class Container<T> : IDisposable
    {
        [ProtoMember(1)]
        public int prop1 { get; set; }

        [ProtoMember(2)]
        public T payload { get; set; }

        public void Dispose() { }
    }

    [ProtoContract]
    public class AnotherDerived : SomeDerived, IDisposable
    {
        [ProtoMember(1)]
        public int AnotherDerivedProp { get; set; }
        public override void Dispose() { }
    }

    [ProtoContract]
    public class SomeDerived : SomeBase, IDisposable
    {
        [ProtoMember(1)]
        public int SomeDerivedProp { get; set; }

        public override void Dispose() { }
    }

    [ProtoContract]
    public class SomeBase : IDisposable
    {
        [ProtoMember(1)]
        public int SomeBaseProp { get; set; }

        public virtual void Dispose() { }
    }

    [ProtoContract]
    public class NotInvolved : IDisposable
    {
        [ProtoMember(1)]
        public int NotInvolvedProp { get; set; }
        public void Dispose() { }
    }

    [ProtoContract]
    public class AlsoNotInvolved : IDisposable
    {
        [ProtoMember(1)]
        public int AlsoNotInvolvedProp { get; set; }
        public void Dispose() { }
    }
}

Request

This is minor, but it’d be nice if

  (Container<SomeDerived>)model.DeserializeWithLengthPrefix(...) 

could also be implemented like this

  model.DeserializeWithLengthPrefix<Container<SomeDerived>>(...):

btw: I’m starting to dig into the protobuf-net implementation, and I’m starting to notice some interesting methods like this. Something to come back to later I guess:

  public MetaType Add(int fieldNumber, string memberName, Type itemType, Type defaultType);

Discussion:

when I saw the way you could deserialize to an abstract base type in the link above, I thought, yes, that’s closer to what was thinking. Could we deserialize to the open generic Container<> first, and then cast more specifically if we need to in different assemblies. Maybe I’m getting mixed up a little here.

You could think of it in terms of Tupple<TBase,TPayload>. Or a variation like Tupple<TBase,Lazy<TPayload>> maybe. It’s not that different to List<T>. There are some TreeTypeThings<T> that I have too, but I don’t need to serialize/deserialize them (yet).

I had a non-generic sequence working, so it isn’t a show stopper. My first implementation could be more efficient. I think I can do better on that with existing protobuf-net features though.

I like the cleaner generic way of working with these ideas. Although I can get to the same destination manually, Generics make other things possible.

re: clarification

everything can be defined ahead of time by the caller. (btw: You’ve got me thinking about the run-time only scenario now, but no, I don’t need that).

  • 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-24T03:50:33+00:00Added an answer on May 24, 2026 at 3:50 am

    So I think the question boils down to “can I use an open generic type as a template for a protobuf model”, in which case the answer is “maybe”. At the moment, it would see BasicMsg<Foo> and BasicMsg<Bar> as different types, and it would default to using the attribute type model, since it won’t recognise them as being defined by [typeof(BasicMsg<>)]. If they have attributes, it’ll probably work, but I don’t think that was your intention, right?

    This is an interesting scenario, and I’m open to discussion on it. However, one particular concern I’d have here is that the nature of generics in .NET means this would require runtime participation, i.e. RuntimeTypeModel. I don’t think I could get it working on pre-compiled TypeModel without using MakeGenericMethod which I really want to avoid for both platform and performance reasons. But as a full-.NET runtime-only feature, it looks interesting.

    (clarification on the above; if the caller could define all the T for BasicMsg<T> ahead of time, it becomes slightly more doable; then it really comes down to a model template metaphor)

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

Sidebar

Related Questions

I am using the latest version of protobuf-net with VS2008 integration. I have created
I was using a version of v2 of protobuf-net from a few weeks ago
We are currently using Mysql version 5.0 in our application and are thinking of
I'm using an older version of ASP.NET AJAX due to runtime limitations, Placing a
I'm using an Informix (Version 7.32) DB. On one operation I create a temp
I developing ASP.NET application using a Swedish version of Windows XP and Visual studio
When using the generated Python code from our protobuf classes, we get this error:
I am using protobuf-net on the project I am working on for the data
I am using protobuf-net r282 and when I call Serialize I get the InvalidOperationException
We are using MySQL version 5.0 and most of the tables are InnoDB. We

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.