Replace LenB with C #

I have a program ported from VB6 to C #. It controls various equipment in an industrial environment, so it must create and calculate the length in bytes of messages sent to the hardware. These messages are created from structures consisting of many primitive data types.

The original VB6 code is widely used by LenB. I am thinking of using System.Runtime.InteropServices.Marshal.SizeOf () instead. Is this a good choice to replace LenB?

In addition, the source program used several LenB places on the same line, for example

inputlen = LenB(sequenceBytes) - LenB(headerBytes) - LenB(crcBytes)

Replacing each of them with "system.Runtime.InteropServices.Marshal.SizeOf" will result in a long, hard to read line. C # doesn't have preprocessor macros, so is there a way to use the using statement or something to eliminate all of these qualifiers in order to shorten the string? Or just write a method?

+4
source share
1 answer

In C #, this expression evaluates the size in bytes of a given Type:

sizeof(Type)

C # doesn't have preprocessor macros, so is there a way to use the using statement or something to eliminate all of these qualifiers in order to shorten the string? Or just write a method?

Just add System.Runtime.InteropServicesfrom the sentences usingat the beginning of your code file:

using System.Runtime.InteropServices;

Marshal.SizeOf() .

+4

All Articles