Reputation: 3715
According to the Marshal.SizeOf() MSDN docs:
Warning
This API is now obsolete.
What is the proper way to get the struct size without using this obsolete function? We can assume .NET Core 2.1 and C# 8.
I need to use this on a generic type, so my code reads similar to this pseudocode:
...<T> where T : struct
{
...
// Note that T will have [StructLayout(LayoutKind.Sequential, Pack = 1)]
assert(something == Marshal.SizeOf(typeof(T)) ...)
}
Upvotes: 0
Views: 86
Reputation: 38
You can use Marshal.SizeOf<T>()
For example:
struct DDType
{
public int No1 { get; set; }
public int No2 { get; set; }
public string Name { get; set; }
}
Marshal.SizeOf<DDType>();
Upvotes: 1