Get SizeOf an unmanaged class including their flields.

Hi everyone,
I’d like to get the SizeOf an unmanaged class including their fields.

public class Data {
int a =0;
int b =0;
}

using System.Runtime.InteropServices;
Debug.Log(Marshal.SizeOf(sample));
Does not work.

Marshal.SizeOf(sample.a)) + Marshal.SizeOf(sample.b))
Works, but that’s ugly because on changing the class fields I have to change the code as well.

Is there any other way to get the class size without collect each field in a class?
Thank you.

With Reflection you could step through all fields in sequence and size them, without needing to know how many there are
eg c# - How to get the list of properties of a class? - Stack Overflow

The size of a class variable is just a pointer.

The size of a struct is probably what you meant. Did you try declaring Data as a struct instead?

But what is your objective?

What exactly do you think “unmanaged” means, when you’re asking for the size of a C# class, which is obviously “managed”?