Search Unity

Get SizeOf an unmanaged class including their flields.

Discussion in 'Scripting' started by Quatum1000, Jan 19, 2019.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    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.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    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"?