Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计算对象的字节大小_C#_Directx_Slimdx - Fatal编程技术网

C# 计算对象的字节大小

C# 计算对象的字节大小,c#,directx,slimdx,C#,Directx,Slimdx,我开始使用directx和c#中的slimdx包装器。对于许多方法,必须计算对象的大小,例如在缓冲区和绘图调用的情况下。特别是,“跨距”是连续元素之间的字节数 到目前为止,我传递的数据是一个向量3,长度为12字节。因此,使用的缓冲区大小是元素数*12。在这个简单的例子中,很容易看到应该是什么大小。但是,对于更复杂的示例,我应该如何计算它?例如: struct VertexType { public Vector3 position; public Vector4 color; }

我开始使用directx和c#中的slimdx包装器。对于许多方法,必须计算对象的大小,例如在缓冲区和绘图调用的情况下。特别是,“跨距”是连续元素之间的字节数

到目前为止,我传递的数据是一个向量3,长度为12字节。因此,使用的缓冲区大小是元素数*12。在这个简单的例子中,很容易看到应该是什么大小。但是,对于更复杂的示例,我应该如何计算它?例如:

struct VertexType
{
    public Vector3 position;
    public Vector4 color;
}
这个尺寸是(12+16)吗?它被安排在一个结构中这一事实是否增加了元素的大小

我尝试过使用sizeof,但这会抛出一个错误,指出对象不是预定大小。正确的方法是什么?

试试Marshal.SizeOf-


也许可以尝试使用
System.Runtime.InteropServices.Marshal.SizeOf
instead在完整的错误消息中看到了这一点。但是,它需要该类型的实例。报告的大小是否与所有实例一致?我还对为什么尺寸不是预先确定的感兴趣
using System.Runtime.InteropServices;

VertexType v = new VertexType;
Marshal.SizeOf(typeof(VertexType)); //For the type
Marshal.SizeOf(v); //For an instance