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 7048775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:55:56+00:00 2026-05-28T02:55:56+00:00

I’m building a series of classes in an attempt to reduce the manual coding

  • 0

I’m building a series of classes in an attempt to reduce the manual coding of specific class types from the mainline code.

Whichever way I look there is always somewhere that requires manual coding of the specific class type.

I was hoping that I could use reflection/activator etc to be able to use the work done in the constructor to be able to return the classtypes (of the correct type) without needing to do the large (although the example here has been reduced) select/switch statement that is in GetPacket.

I know this is VB.Net, but the project was already written in that language, I don’t mind if you post C# examples I’ll just convert them.
But please don’t retag as a VB.Net question because it’s not about language it’s about how to do it within the framework.

Imports ProtoBuf

Public Class CompatiblePackets
    Inherits Dictionary(Of Packet.PacketType, Base)

    Public Sub New()
        Dim theAssembly As Assembly = Assembly.GetExecutingAssembly
        For Each t As Type In theAssembly.GetTypes
            If t.BaseType Is GetType(Base) Then
                Dim p As Base = CType(t.Assembly.CreateInstance(t.FullName), Base)
                Me.Add(p.PacketTypeIndicator, p)
            End If
        Next
    End Sub

    Public Function GetPacket(id As PacketType, data As Stream) As Base
        Dim activePacket As Base
        If Me.ContainsKey(id) Then
            activePacket = Me(id)
        Else
            activePacket = Me(PacketType.Generic)
        End If
        Try
            Select Case id
                Case PacketType.AcknowledgeBulk
                    Return GetPacket(Of AcknowledgeBulk)(activePacket, data)
                Case PacketType.Generic
                    Return GetPacket(Of Generic)(activePacket, data)
                Case PacketType.Identification
                    Return GetPacket(Of Identification)(activePacket, data)

    '''There are so far about 20 more packet types in the real code.

                Case Else
                    'unknown type "Computer says No!"
            End Select
        Catch ex As Exception
            If data.GetType Is GetType(MemoryStream) Then
                Debug.Print(Core.Text.OutputData(CType(data, MemoryStream).ToArray))
            End If
            Throw
        End Try
        Debug.Print("Wtf - " & id.ToString())

        Return New NoOperation
    End Function

    Private Function GetPacket(Of t)(activePacket As Packet.Base, data As Stream) As t
        Return Serializer.Deserialize(Of t)(data)
    End Function
End Class
  • 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-28T02:55:56+00:00Added an answer on May 28, 2026 at 2:55 am

    If I understand the question correctly, it looks like you should be using Serializer.NonGeneric; this has various methods for passing the Type in manually and without using generics, including Serialize and Deserialize.

    Note that for this scenario, Serializer.NonGeneric also has a specific API for heterogeneous header-marked messages, which you may find useful for a network-stream scenario:

    static void Main() {
        // memory-stream only for example - would work fine with NetworkStream too
        using(var ms = new MemoryStream()) {
            // this is just to simulate an incoming network-stream
            Serializer.SerializeWithLengthPrefix(ms, new Foo { Name = "abc" },
               PrefixStyle.Base128, 1); // tag 1 for Foo
            Serializer.SerializeWithLengthPrefix(ms, new Bar { Value = 123 },
               PrefixStyle.Base128, 2); // tag 2 for Bar
            ms.Position = 0;
    
            // now we'll consume it
    
            //first setup some map of 1=Foo, 2=Bar - any mechanism will suffice
            var typeMap = new Dictionary<int, Type>{
                {1, typeof(Foo)},
                {2, typeof(Bar)}
            };
            Serializer.TypeResolver resolver = i => {
                Type type;
                return typeMap.TryGetValue(i, out type) ? type : null;
            };
            object obj;
            while(Serializer.NonGeneric.TryDeserializeWithLengthPrefix(
                ms, PrefixStyle.Base128,resolver,out obj))
            {
                Console.WriteLine(obj);
            }
            Console.WriteLine("<EOF>");
    
        }
    }
    [ProtoContract]
    class Foo {
        [ProtoMember(7)]
        public string Name{ get; set; }
    
        public override string ToString() { return "Foo: " + Name; }
    }
    [ProtoContract]
    class Bar {
        [ProtoMember(4)]
        public int Value { get; set; }
    
        public override string ToString() { return "Bar: " + Value; }
    }
    

    This outputs:

    Foo: abc
    Bar: 123
    <EOF>
    

    In v2, note that TypeModel exposes the non-generic API as the primary interface (the Serializer API simply acts as a proxy to the default model).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.