Search code examples
c#objectsendtcpbinary-serialization

Send myObject by TCP/IP


I have a problem from send myObject by TCP/IP.

My ArchMap

public class ArchMap
{
    public string DetalCode { get; set; }
    public List<Arch> Archs { get; set; }
}

and Arch

public class Arch
{
    public string ModulName { get; set; }
    public string PartName1 { get; set; }
    [...]
}

I want to send this list to TcpClient but I don't know how convert my list to byte[].

I try

   var bf = new BinaryFormatter();
   var ms = new MemoryStream();
   bf.Serialize(ms, xarchList);

but I get error:

Additional information: Typ 'Arch_Sender.Model.ArchMap' in Assembly 'ArchSender.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable


Solution

  • Please consider using Serializable attribute like this:

    ArchMap

    using using System.Runtime.Serialization;
    
    [Serializable]
    public class ArchMap
    {
        public string DetalCode { get; set; }
        public List<Arch> Archs { get; set; }
    }
    

    Arch

    using System.Runtime.Serialization;
    
    [Serializable]
    public class Arch
    {
        public string ModulName { get; set; }
        public string PartName1 { get; set; }
        [...]
    }
    

    More info: https://msdn.microsoft.com/en-us/library/ms973893.aspx