Question

Given a FieldInfo object and an object, I need to get the actual bytes representation of the field. I know that the field is either int,Int32,uint,short etc.

How can I get the actual byte representation? BinaryFormatter.Serialize won't help, since it'll give me more information than I need (it also records type name etc.). The Marshal class does not seem to have facilities to use bytes array (but maybe I'm missing something).

Thanks

Was it helpful?

Solution

You may also try code like the following if what you actually want is to transfer structures as a byte array:

int rawsize = Marshal.SizeOf(value);
byte[] rawdata = new byte[rawsize];
GCHandle handle = GCHandle.Alloc(rawdata, GCHandleType.Pinned);
Marshal.StructureToPtr(value, handle.AddrOfPinnedObject(), false);
handle.Free();

This converts the given object value to the byte array rawdata. I've taken this from code I previously wrote, and you may need to adapt it to your needs to make it actually work. I used it for communication with some hardware with user-defined structures, but it should work for built-in types as well (after all, they're structures, aren't they?)

To make structure members properly aligned, use the StructLayout attribute to specify one-byte-alignment:

[StructLayout(LayoutKind.Sequential, Pack = 1)]

And then use the MarshalAs attribute as needed for fields, e.g. for inline arrays:

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
byte[] _state;

The code to get the structure back from the byte array is something like this:

public T GetValue<T>()
{
    GCHandle handle = GCHandle.Alloc(RawValue, GCHandleType.Pinned);
    T structure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), 
                      typeof(T));
    handle.Free();
    return structure;
}

Of course you'll need to know the type you want for this to work.

Note that this will not handle endianness for itself. In my project, most fields were one byte only, so it didn't matter, but for the few fields where it did, I just made the fields private and added public properties that would take care of the endianness (Jon Skeet's link from a comment to his answer may help you, I wrote some utility functions for this since I needed only few).

When I needed this, I created a Message class that would store the raw value (hence the GetValue method, the code at the top is actually the body of a SetValue method) and had some nice convenience method to get the value formatted etc.

OTHER TIPS

Use BitConverter.GetBytes()

You'll first have to convert the value to it's native type, than use BitConverter to get the bytes:

byte[] Bytes;

if (valType == typeof(int))
{
    int intVal = (int) GetFieldValue(....);
    Bytes = BitConverter.GetBytes(intVval);
} 
else if (valType == typeof(long))
{
    int lngVal = (long) GetFieldValue(....);
    Bytes = BitConverter.GetBytes(lngVal);
} else ....

Do you mean the definitive in-memory representation? BitConverter.GetBytes (with an overload suitably chosen by reflection) will return you a byte representation, but not necessarily what it is currently in memory.

Perhaps if you give more information about why you want this, we'll be better able to help you.

EDIT: I should add that in any sensible case I can think of, BitConverter will give you the same representation as in memory - but there may be odd situations involving endianness and possibly weird architectures with different floating point representations which could give strange results.

EDIT: Here's a complete sample program demonstrating how you might go about it:

using System;
using System.Reflection;

public class Test
{
    public int x = 300;

    static void Main()
    {
        Test instance = new Test();
        FieldInfo field = typeof(Test).GetField("x");

        MethodInfo converter = typeof(BitConverter).GetMethod("GetBytes", 
            new Type[] {field.FieldType});

        if (converter == null)
        {
            Console.WriteLine("No BitConverter.GetBytes method found for type "
                + field.FieldType);            
        }
        else
        {
            byte[] bytes = (byte[]) converter.Invoke(null,
                new object[] {field.GetValue(instance) });
            Console.WriteLine("Byte array: {0}", BitConverter.ToString(bytes));
        }        
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top