Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the size of a System.Type value?

Tags:

f#

How to determine the size of a Type value?

let typ = if true then (123).GetType() else "asdf".GetType();;
let sz = sizeof<typ>

  let sz = sizeof<typ>;;
  ----------------^^^
error FS0039

One solution is to use Marshal.SizeOf(obj), but may be another solution exists?

like image 231
Andrey Kovbovich Avatar asked Mar 20 '14 23:03

Andrey Kovbovich


People also ask

How do you find the size of a data type?

The size of a data type is given by (name of datatype).SIZE. The maximum value that it can store is given by (Name of data type).MAX_VALUE. The minimum value that it can store is given by (Name of data type).MIN_VALUE.

How to print the size and value of a data type?

The minimum value that it can store is given by (Name of data type).MIN_VALUE. Always write first word of data type in capital. 1. If you want to print the size of float data type, use Float.SIZE 2. If you want to print the size and value of the Byte use the following code 3.

How to find the size of data types in C language?

The range of data types can be found by manually or using <limits.h> and <float.h> The size of data types in C is dependent on the compiler or you can say that the system architecture i.e. 32-bit compiler or 64-bit compiler. The size of data type int is 2 byte in 32-bit architecture or 4 bytes in 64-bit architecture.

How to get size and maximum value of data types in Java?

How to Get Size, Minimum, and Maximum Value of Data Types in Java? The size of a data type is given by (name of datatype).SIZE. The maximum value that it can store is given by (Name of data type).MAX_VALUE.


1 Answers

I think Marshal.SizeOf is what you are looking for here. Note that there are two overloads of the method.

  • Marshal.SizeOf(t) (MSDN) takes System.Type as an argument and returns the size of the type that is represented by the System.Type value (I believe this is what you want.)

  • Marshal.SizeOf(structure) (MSDN) takes any object and returns the size of the structure based on the runtime type.

The sizeof<'T> construct in F# works only when the type is statically know, but then it generates nice single sizeof IL instruction (as you can see from the source code).

like image 198
Tomas Petricek Avatar answered Nov 03 '22 18:11

Tomas Petricek