Hi Pierre, There are a couple of methods you can use depending on what exactly you are trying to achieve.
One method involves uses the serialization to do all of the work for you. Mike Woodring covered this a while ago in his post: http://discuss.develop.com/archives/wa.exe?A2=ind0101D&L=DOTNET&P=R16111 &I=-3 Alternatively, if your ultimate goal is to pass the data through an iterop call (PINVOKE, etc.) you could look at leveraging the interop services further. You won't get a true dynamic_cast type behaviour from this method because you're required to know the type before the calls are made, but it will allow you to write generic code. If the struct is blittable, you could simply pin the struct (see System.Runtime.InteropServices.GCHandle.Alloc) and pass it directly to the method call. Also, the PtrToStructure and StructureToPtr methods on the System.Runtime.InteropServices.Marshal class can be quite useful for this kind of thing. Cheers, Mark. > -----Original Message----- > From: Moderated discussion of advanced .NET topics. > [mailto:[EMAIL PROTECTED]] On Behalf Of > Pierre Greborio > Sent: Thursday, 19 September 2002 7:49 AM > To: [EMAIL PROTECTED] > Subject: [ADVANCED-DOTNET] Casting > > > Hi, > to transform a structure to array of byte[] I am using the following > procedure: > > public static unsafe byte[] StructToByte(MySimpleStruct > simpleStruct) { > byte[] arr = new byte[ > System.Runtime.InteropServices.Marshal.SizeOf(simpleStruct) ]; > fixed( byte* parr = arr ) > { > *((MySimpleStruct*)parr) = simpleStruct; > } > return arr; > } > > in order to keep it more generic, I can change the parameter > to ValueType. The problem raises on the casting: > > *((MySimpleStruct*)parr) = simpleStruct; > > Is it possible to make somwthing like a dynamic_cast ? > > Thanks > Pierre > > ----------------------------------------------- > Pierre Greborio > [EMAIL PROTECTED] > http://www.pierregreborio.it <http://www.pierregreborio.it/> > ----------------------------------------------- > > > > > > You can read messages from the Advanced DOTNET archive, > unsubscribe from Advanced DOTNET, or subscribe to other > DevelopMentor lists at http://discuss.develop.com. > You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.
