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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:07:22+00:00 2026-05-10T22:07:22+00:00

I have an array of different type objects and I use a BinaryWriter to

  • 0

I have an array of different type objects and I use a BinaryWriter to convert each item to its binary equivalent so I can send the structure over the network.

I currently do something like

for ( i=0;i<tmpArrayList.Count;i++) {    object x=tmpArrayList[i];    if (x.GetType() ==  typeof(byte))    {       wrt.Write((byte)x);    }    ........ 

The problem is that if miss a type them my code might break in the future.

I would like to do something like.

object x=tmpArrayList[i]; wrt.Write(x); 

but it doesn’t work unless I do each cast.

Edit:

After consulting the answers this is what I came up with for the function. For testing this function sends the array to syslog.

  private void TxMsg(ArrayList TxArray,IPAddress ipaddress)   {      Byte[] txbuf=new Byte[0];      int sz=0;       // caculate size of txbuf      foreach (Object o in TxArray)      {         if ( o is String )          {            sz+=((String)(o)).Length;         }         else if ( o is Byte[] )         {            sz+=((Byte[])(o)).Length;         }         else if ( o is Char[] )         {            sz+=((Char[])(o)).Length;         }         else // take care of non arrays         {            sz+=Marshal.SizeOf(o);         }      }      txbuf = new Byte[sz];       System.IO.MemoryStream stm_w = new System.IO.MemoryStream( txbuf, 0,txbuf.Length);      System.IO.BinaryWriter wrt = new System.IO.BinaryWriter( stm_w );       foreach (Object o in TxArray)      {         bool otypefound=false;         if (o is String) // strings need to be sent one byte per char         {            otypefound=true;            String st=(String)o;            for(int i=0;i<st.Length;i++)            {               wrt.Write((byte)st[i]);            }         }         else         {            foreach (MethodInfo mi in typeof(BinaryWriter).GetMethods())            {               if (mi.Name == 'Write')               {                  ParameterInfo[] pi = mi.GetParameters();                  if ((pi.Length == 1)&&(pi[0].ParameterType==o.GetType()))                  {                     otypefound=true;                     mi.Invoke(wrt, new Object[] { o });                  }               }            }         }         if(otypefound==false)         {            throw new InvalidOperationException('Cannot write data of type ' + o.GetType().FullName);         }      }      IPEndPoint endpoint = new IPEndPoint(ipaddress, 514); //syslog port      UdpClient udpClient_txmsg = new UdpClient();      udpClient_txmsg.Send(txbuf, txbuf.Length,endpoint); // send udp packet to syslog                } 
  • 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. 2026-05-10T22:07:23+00:00Added an answer on May 10, 2026 at 10:07 pm

    Here is a solution for BinaryWriter that uses reflection.

    This basically scans BinaryWriter for methods named Write that takes exactly one parameter, then builds a dictionary of which method handles which type, then for each object to write, finds the right method and calls it on the writer.

    Dirty, and you should probably look for better ways of doing the whole thing (not just the writing part), but it should work for your current needs:

    using System.IO; using System; using System.Reflection; using System.Collections.Generic; namespace ConsoleApplication14 {     public class Program     {         public static void Main()         {             Dictionary<Type, MethodInfo> mapping = new Dictionary<Type, MethodInfo>();             foreach (MethodInfo mi in typeof(BinaryWriter).GetMethods())             {                 if (mi.Name == 'Write')                 {                     ParameterInfo[] pi = mi.GetParameters();                     if (pi.Length == 1)                         mapping[pi[0].ParameterType] = mi;                 }             }              List<Object> someData = new List<Object>();             someData.Add((Byte)10);             someData.Add((Int32)10);             someData.Add((Double)10);             someData.Add((Char)10);             someData.Add('Test');              using (FileStream file = new FileStream(@'C:\test.dat', FileMode.Create, FileAccess.ReadWrite))             using (BinaryWriter writer = new BinaryWriter(file))             {                 foreach (Object o in someData)                 {                     MethodInfo mi;                     if (mapping.TryGetValue(o.GetType(), out mi))                     {                         mi.Invoke(writer, new Object[] { o });                     }                     else                         throw new InvalidOperationException('Cannot write data of type ' + o.GetType().FullName);                 }             }         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The docs should be on the nVidia developer site, though… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer IEnumerable<String> GetLoadedAssemblies() { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { yield… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer You might want to look at NHibernate, LINQ to SQL,… May 11, 2026 at 11:44 pm

Related Questions

I have a procedure that expects a parameter of type TObject, something like this:
I have a couple of classes that want to pass each other some information
My problem is the following. I have a method which simply takes an XML
Scenario: I'm currently writing a layer to abstract 3 similar webservices into one useable

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.