class A
{
private int a;
private double b;
}如何能像C++那样计算类A的大小,用Sizeof(A)出错,请问应该怎么做?

解决方案 »

  1.   

    运行时错误,不知道怎么解决~~~~~下面是我的程序:using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;namespace Test
    {    class A
        {
            private int a;
            private double b;
        }    class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(Marshal.SizeOf(typeof(A)));
                
            }
        }
    }未处理的异常:  System.ArgumentException: 类型“类类型.A”不能作为非托管结构进行封送处理;无法计算有意义的大小或偏移量。
       在 System.Runtime.InteropServices.Marshal.SizeOf(Type t)
      

  2.   

    //声明成结构类型struct A
    {
        private int a;
        private double b;
    }