I suppose only a few of here really ever used this method from the marshall namespace,
for those that are not familaire to the marshalling methods and its meaning,
please refer to the wiki for more explaination.
http://en.wikipedia.org/wiki/Marshalling...r_science)
Anyway, to continue i used it for a series of tests in some of my projects,
and i was bothered by the fact that the SizeOf() is restricted to unmannged code only or basic types.
So i begun my hunting for a way to bypass the compiler restriction wich i found out was quit easy actually.
There was a (stackoverflow.com) thread about this matter and i got my solution.
The guy in qeustion who broke down the easiest way was called dasblinkenlight (Thanks dude ^^).
Now if you play arround with the SizeOf you would most likely into the following exception:
In this situation you could do a little trick to obtain the size of that structure by measuring the memory usage with the garbage collector using the GetTotalMemory() method.
But its been said this is not thread safe, and i do work with threads alote,
so i don't wanna put crap infront of my plans ahead, we need to come up with a more cooked-up trick up our sleeve here.
Here are my test results and my findings
![[Image: h6nxhxw.png]](https://i.imgur.com/h6nxhxw.png)
Reference table from msdn:
The solution is simple, we just create our own SizeOf() method and we emit the IL opcode for it and create a delegate followed by invoking it.
Works just as fine, no sweat has been broken here 👍
Source solution:
I hope it helps anybody out there, as it did for me.
- Barret 🎉
for those that are not familaire to the marshalling methods and its meaning,
please refer to the wiki for more explaination.
http://en.wikipedia.org/wiki/Marshalling...r_science)
Quote:Marshalling is the process of transforming the memory representation of an object to a data format suitable for storage or transmission, and it is typically used when data must be moved between different parts of a computer program or from one program to another. Marshalling is similar to serialization
Anyway, to continue i used it for a series of tests in some of my projects,
and i was bothered by the fact that the SizeOf() is restricted to unmannged code only or basic types.
So i begun my hunting for a way to bypass the compiler restriction wich i found out was quit easy actually.
There was a (stackoverflow.com) thread about this matter and i got my solution.
The guy in qeustion who broke down the easiest way was called dasblinkenlight (Thanks dude ^^).
Now if you play arround with the SizeOf you would most likely into the following exception:
Quote:Type cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
In this situation you could do a little trick to obtain the size of that structure by measuring the memory usage with the garbage collector using the GetTotalMemory() method.
Code
System.GC.GetTotalMemoryBut its been said this is not thread safe, and i do work with threads alote,
so i don't wanna put crap infront of my plans ahead, we need to come up with a more cooked-up trick up our sleeve here.
Here are my test results and my findings
![[Image: h6nxhxw.png]](https://i.imgur.com/h6nxhxw.png)
Reference table from msdn:
- sizeof(sbyte) 1 bytes
- sizeof(byte) 1 bytes
- sizeof(short) 2 bytes
- sizeof(ushort) 2 bytes
- sizeof(int) 4 bytes
- sizeof(uint) 4 bytes
- sizeof(long) 8 bytes
- sizeof(ulong) 8 bytes
- sizeof(char) 2 bytes
- (Uni) sizeof(float) 4 bytes
- sizeof(double) 8 bytes
- sizeof(decimal) 16 bytes
- sizeof(bool) 1 bytes
The solution is simple, we just create our own SizeOf() method and we emit the IL opcode for it and create a delegate followed by invoking it.
Works just as fine, no sweat has been broken here 👍
Source solution:
Code
Public Shared Function SizeOf(Instance As Object) As Integer
Dim Method As New DynamicMethod("SizeOf", GetType(UInteger), Nothing, True)
Dim ILGen As ILGenerator = Method.GetILGenerator()
ILGen.Emit(OpCodes.Sizeof, Instance.GetType)
ILGen.Emit(OpCodes.Ret)
Return CInt(Method.CreateDelegate(GetType(Func(Of UInteger))).DynamicInvoke)
End FunctionI hope it helps anybody out there, as it did for me.
- Barret 🎉

