I'm trying to use the Read function, and I was wondering if it is possible to pass in and cast one of my own objects to a byte  and read it into that structure directly.
Basically I have a class with some properties, I initialize it and what not then I want to read it's contents from a binary file using the read function.
So basically my class has some ints longs doubkes etc...all public
then I want to
file.Read((byte)myclass, (int)file.BaseStream.Position, (int)Marshal.SizeOf(myclass));
but the cast from myclass to byte fails.
I get the same error in reverse direction too, so if I have byte myArray and class myClass
myClass object = (myClass)myArray;
the conversion from byte to myClass fails also.
Originally Posted by rags_to_riches
thanks, but this is actually the solution I found after A LOT of searching...it wasn't really even in my Deitel C# book
we can do things like
myClass thing = new myClass();
MemoryStream myClassMS= new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
if we want a bytes array from thing we do the following
byte thingBytes = myClassMS.GetBuffer();
and if we want to cast a myClass object to another type we can do this
MemoryStream myClassMS2= new MemoryStream( thing);
long longThings = (long)formatter.Deserialize(myClassMS2);
very useful when converting C or C++ code that uses pointer arithmetic to C# without changing a lot.
I hope this can be usefull to someone else, I spent a lot of time looking for it.
Ok, so the above doesn't work exactly as desired. The problem I ran into with this is that I already have the byte  that I read in from the bin file. The code above only works if you have existing objects convert them to bytes and then cast them back or vice versa. I'm starting with bytes and need to get to my object type.
If I can't resolve this elegantly using some C# bin tools, I'm going to be forced to create a function in my classes that takes an array of bytes, and manually maps them to each attribute of the class
Did you find easy way to fill structue or classes using Binary reader
For now, I thing that the best is to write some native C function in a dll and call the dll passing pointers.
C#, and more specifically the .Net Framework, provide allot of binary functionality and is a quite involved process as it is in any other language. An excellent place to start your research is definitively MSDN.
The MSDN Express Edition is free and incorporates local help access as well as online help access giving you a one, two punch in your research on a .Net topic.
As far as binary operations are concerned, .Net 3.x is going to allow allot more accessibility than previous versions of the framework by convention so an upgrade might be in order for those of you that have not already done so.
The .Net Framework as it stands supports binary operations, but to what extent is the question and one that will not be answered until necessity beckins.
In closing, do not paint yourself into a binary corner...have a back up plan!
Develop your system prior to incorporating explicit binary operations and just update your system as new binary operations are added or discovered preventing hacks and workarounds muddying up your system and its code.