Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, as example i have a class Client :
C#
Class Client
   {
        public Client(string name)
          {
            Name = name;
          }
        private string Name
          {
            get;
            set;
          }
    }
/*This is not my class case, i might have any other class, i need a general solution not depending on class internal structures and fields */

so, i used:
C#
Client CLI = new Client("John");

i need to convert my data object CLI to a byte[] equivalent representation
how can i ?
thank's for any help :)
Posted
Comments
F. Aro 7-Apr-12 2:15am    
i need something like :

byte[] CL = ConvertFrom(CLI);//ConvertFrom a written method to convert Object CLI to a byte...

1 solution

It's not as simple as that: it depends on what you class contains. If it has references to other classes (as it probably does) then you need to include those as well.
To convert it to a raw byte array is pretty simple:
C#
private static byte[] ToByteArraySimple(object o)
    {
    int length = Marshal.SizeOf(o);
    byte[] bytes = new byte[length];
    IntPtr start = Marshal.AllocHGlobal(length);
    Marshal.StructureToPtr(o, start, false);
    Marshal.Copy(start, bytes, 0, length);
    Marshal.FreeHGlobal(start);
    return bytes;
    }

But to be safe, mark your class with the Serializable attribute (and all referenced classes) and use a Binary Formatter:
C#
private static byte[] ToByteArrayBetter(object o)
    {
    var formatter = new BinaryFormatter();
    using (var stream = new MemoryStream())
        {
        formatter.Serialize(stream, o);
        return stream.ToArray();
        }
    }
 
Share this answer
 
Comments
F. Aro 7-Apr-12 3:28am    
Thank for help,
(my namespace is "Test",contains class "Client")
i have some trouble (Exceptions thrown), i did :

Client Cl = new Client("John");
byte[] b = ToByteArrayBetter(Cl);

when i used ToByteArrayBetter :
Type 'Test.Client' in Assembly 'Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

even when i used ToByteArraySimple :

Type 'Test.Client' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.

how can i handle ?
OriginalGriff 7-Apr-12 3:30am    
Did you add the Serializable attribute to your class?
F. Aro 7-Apr-12 3:39am    
Aha it's ok, i missed to add it !
it works only with ToByteArrayBetter.
other method still throwing same exception.
anw thank you, i think this method is working fine with me :)
F. Aro 7-Apr-12 3:44am    
by the way i asked a question before, but no one gave me a solution, if you have any idea:

i need a funtion that executes bytecode :
if i load byte[] b of an executable (byte[] b = File.ReadAllBytes(string path))
how can i execute the executable using the byte[] b
//i don't want to use Process.Start ...
//i need to execute an executable by a byte[] variable
OriginalGriff 7-Apr-12 4:00am    
Why on earth would you want to?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900