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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:15:22+00:00 2026-06-15T07:15:22+00:00

i want to access the bytes of an object in C# okey for example

  • 0

i want to access the bytes of an object in C# okey for example :

Serializing an class in WCF the serializer read the all bytes of class object and Finally SOAP message !

Some Thing Like This You Know A Way To Read object Bytes And Recreate object by its Bytes

//x_obj created and we allocate an address & size of RAM (memory) to it Now its byts structure is in the RAM

X_calss x_obj = new X_class(); 

//and now we want to read the x_obj bytes in RAM
unsafe byte[] READ(X_class x_obj){
 xobj_pointer = &x_obj;//pointer to address of obj in RAM
 byte[] xobj_bytes = read_from(xobj_pointer,sizeof(x_obj));//Some Way To Read Byte of x_obj
 return xobj_bytes;
}
// and now we want to recreate class by it bytes stream
unsafe X_class Creative(byte[] xobj_bytes){
 x_pointer = Memory_allocate(sizeof(X_class));//reserve an address of RAM
 write_to(x_pointer,xobj_bytes);//write bytes to RAM 
 X_class x_obj = (X_class)*(x_pointer);//recreate the class by content of pointer
 return x_obj;
}
  • 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-06-15T07:15:23+00:00Added an answer on June 15, 2026 at 7:15 am

    While agreeing with both Jon Skeet and ValtasarIII, getting access to the raw bytes of a variable is possible (in a horrible kind of way) if your data types are structs (with known layouts), contain only value types and you’re allowing unsafe code.

    What follows is certainly not a technique that you’d want to use to cross machine boundaries (nor should it really be used at all).

    Given a structure called TestStruct defined like so

    [StructLayout(LayoutKind.Sequential)]
    public struct TestStruct
    {
        public int A;
        public int B;
    }
    

    The raw bytes of its contents may be obtained like this

    private static unsafe byte[] GetBytes(TestStruct item)
    {
        //Figure out how big TestStruct is
        var size = Marshal.SizeOf(item);
        //Make an array large enough to hold all the bytes required
        var array = new byte[size];
        //Get a pointer to the struct
        var itemPtr = &item;
        //Change the type of the pointer from TestStruct* to byte*
        var itemBytes = (byte*) itemPtr;
    
        //Iterate from the first byte in the data to the last, copying the values into our
        //    temporary storage
        for (var i = 0; i < size; ++i)
        {
            array[i] = itemBytes[i];
        }
    
        //Return the bytes that were found in the instance of TestStruct
        return array;
    }
    

    And we can build a new one like this

    private static unsafe TestStruct Reconstitute(IList<byte> data)
    {
        //Figure out how big TestStruct is
        var size = Marshal.SizeOf(typeof(TestStruct));
    
        //If the data we've been presented with is either too large or too small to contain
        //    the data for exactly one TestStruct instance, throw an exception
        if (data.Count != size)
        {
            throw new InvalidOperationException("Amount of data available is not the exact amount of data required to reconstitute the item");
        }
    
        //Make our temporary instance
        var item = new TestStruct();
        //Get a pointer to our temporary instance
        var itemPtr = &item;
        //Change the type of the pointer to byte*
        var itemBytes = (byte*) itemPtr;
    
        //Iterate from the first byte in the data to the last, copying the values into our
        //    temporary instance
        for (var i = 0; i < size; ++i)
        {
            itemBytes[i] = data[i];
        }
    
        //Return our reconstituted structure
        return item;
    }
    

    Usage:

    static void Main()
    {
        var test = new TestStruct
        {
            A = 1,
            B = 3
        };
    
        var bytes = GetBytes(test);
        var duplicate = Reconstitute(bytes);
    
        Console.WriteLine("Original");
        PrintObject(test, 1);
    
        Console.WriteLine();
        Console.WriteLine("Reconstituted");
        PrintObject(duplicate, 1);
    
        Console.ReadLine();
    }
    

    And, for completeness, the code for PrintObject

    static void PrintObject(object instance, int initialIndentLevel)
    {
        PrintObject(instance, initialIndentLevel, 4, ' ', new List<object>());
    }
    
    static void PrintObject(object instance, int level, int indentCount, char paddingChar, ICollection<object> printedObjects)
    {
        if (printedObjects.Contains(instance))
        {
            return;
        }
    
        var tabs = "".PadLeft(level * indentCount, paddingChar);
        var instanceType = instance.GetType();
        printedObjects.Add(instance);
    
        foreach (var member in instanceType.GetMembers())
        {
            object value;
            try
            {
                switch (member.MemberType)
                {
                    case MemberTypes.Property:
                        var property = (PropertyInfo) member;
                        value = property.GetValue(instance, null);
                        break;
                    case MemberTypes.Field:
                        var field = (FieldInfo) member;
                        value = field.GetValue(instance);
                        break;
                    default:
                        continue;
                }
            }
            catch
            {
                continue;
            }
    
            if (value == null || value.GetType().IsValueType || value.GetType().ToString() != value.ToString())
            {
                Console.WriteLine("{2}{0}: {1}", member.Name, (value ?? "(null)"), tabs);
    
            }
            else
            {
                var vals = value as IEnumerable;
    
                if (vals != null)
                {
                    var index = 0;
                    var indented = "".PadLeft((level + 1) * indentCount, paddingChar);
                    Console.WriteLine("{2}{0}: {1}", member.Name, value, tabs);
    
                    foreach (var val in vals)
                    {
                        Console.WriteLine("{1}[{0}]:", index++, indented);
                        PrintObject(val, level + 2, indentCount, paddingChar, printedObjects);
                    }
    
                    if (index == 0)
                    {
                        Console.WriteLine("{0}(No elements)", indented);
                    }
                }
                else
                {
                    PrintObject(value, level + 1, indentCount, paddingChar, printedObjects);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to access a integer and a string from a class to all
I want to access a static variable from a static method: #!/usr/bin/env python class
I want to access a class from another project using ClassLoader. How can I
I have one array of bytes. I want to access each of the bytes
I want to access camera to record video to upload, but have come across
I want to access the following path on Ubuntu in my python code: ~/.mozilla/firefox/dh4ytbdj.default/bookmarkbackups
I want to Access Information of Builds of Team Projects to use them in
I want to access java script's return value , which in is in a
I want to access text-shadow property just like we access padding property. Means ,
I want to access the session variable which I declared in the another php

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.