views:

1628

answers:

4

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

+7  A: 

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 ....
James Curran
Nah, do it with reflection :)
Jon Skeet
+2  A: 

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));
        }        
    }
}
Jon Skeet
I was about to post it as well, but it seems that GetBytes does not have an overload that accepts object, i.e. one would still have to write code that converts to the appropriate type first. Or did I miss something?
OregonGhost
No, you haven't missed anything - you'd have to work out the method to call. Easy enough to do though - I'll edit with a code sample.
Jon Skeet
I'm trying to serialize my object to a specific format required by my hardware. I'm having a message object that I serialize with a special method the object have. It looks like:<code>class msg:mbase {uint x;}</code>mbase moves over all fields of msg and serialize them.
And of course ideally I want to controll endianess, ie getBigEndianBytes().
If you want to control endianness, use my EndianBitConverter from http://pobox.com/~skeet/csharp/miscutil
Jon Skeet
See my answer for a solution specific to hardware communication in my last project. Note that serializing the data on yourself may be overkill, you can adjust the structure layout to your need and use properties instead of fields to take care of endianness.
OregonGhost
Also, +1 for this solution to the specific problem of getting raw values of built-in types, though the actual solution might be to use structures for transferring data instead of reflecting over all fields by hand.
OregonGhost
@Downvoter: Care to give a reason?
Jon Skeet
A: 

This is a little clunky, but it's the best I can think of for now.

    static byte[] GetBytes(System.Reflection.FieldInfo fieldInfo, object obj)
    {
        if (fieldInfo.GetType() == typeof(int))
        {
            return BitConverter.GetBytes((int)fieldInfo.GetValue(obj));
        }
        else if (fieldInfo.GetType() == typeof(short))
        {
            return BitConverter.GetBytes((short)fieldInfo.GetValue(obj));
        }
        // etc...
        else
        {
            throw new FormatException();
        }
    }
Jon B
I already wrote a python script that emitts this code. What I wanted is a method that would save me the trouble (think about how many types are there, int uint short ushort byte sbyte etc).
+3  A: 

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.

OregonGhost
Thanks, this is more like I wanted. How did you make sure the structure would be aligned as you want them to be aligned?
I updated the answer to answer this additional question :)
OregonGhost