English 中文(简体)
How to Iterate Through Array in C# Across Multiple Calls
原标题:

We have an application where we need to de-serialize some data from one stream into multiple objects.

The Data array represents a number of messages of variable length packed together. There are no message delimiting codes in the stream.

We want to do something like:

void Decode(byte[] Data)  
{  
    Object0.ExtractMessage(Data);
    Object1.ExtractMessage(Data);
    Object2.ExtractMessage(Data);
    ...
}  

where each ProcessData call knows where to start in the array. Ideally we d do this without passing a DataIx reference in.

To do this in C++ we d just hand around a pointer into the array, and each ProcessData function would increment it as required.

Each object class knows how its own messages are serialized and can be relied upon (in C++) to return the pointer at the beginning of the next message in the stream.

Is there some inbuilt mechanism we can use to do this (without going unsafe)? The operation is high frequency (~10kps) and very lightweight. We also don t want to go copying or trimming the array.

Thanks for your help.

最佳回答

Could you not just pass in and return the array index? That is basically all that a pointer is anyway, an offset from a fixed memory location.

问题回答

Well this sounds like you want a simple stream (E.g. just use MemoryStream as a wrapper around your byte array: stream = new MemoryStream (data)). Just wrap the byte array into a stream and every object reads as much from the stream as it needs and then hands over the stream to the next item. It even has the benefit that you aren t forced to loading the entire byte-array at once.

Other than that you can use pointers in C# exactly the way you did in C++ (though pointers require the unsafe keyword and they are discouraged)

Alternatively you could just pass data and an index variable and then increment the index (which is, in effect, the same as using a pointer but doesn t need unsafe).

How about wrapping the data in a MemoryStream and then passing a StreamReader into the ExtractMessage method?

I guess several things come to mind.

You could simulate the action of the pointer by wrapping the byte[] in a class which also maintained the array offset. Whenever you access the array you would access it thru the class, probably via an accessor method, which returned the next byte and also incremented the offset variable. The class instance could be passed between the different ExtractMessage function calls.

How about using C++/CLI? This would allow you to use familiar C/C++ techniques, and yet be directly callable from C# without the dreaded interop.

Then of course there is the dreaded unsafe option, whereby you obtain a C# pointer to the byte[] and perform the required pointer arithmetic.

You could create a stream from the byte array.

Stream stream = new MemoryStream(data);

Then your processor s could work on streams instead.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签