如何从System.Type中找到结构的大小?

时间:2012-04-03 17:15:40

标签: c# .net types struct sizeof

  

可能重复:
  Get the sizeof a struct given the System.Type

给定System.Type值类型,我如何找到它的大小?

// this works with a type identifier in an unsafe construct:
int size = sizeof(Guid);

// how in this (simplified) case?
Type t = Guid.Empty.GetType();
int size = t.GetSize();           // how?

我想出了通过反射递归枚举成员,但是如果涉及打包,这不会给出正确的大小。

现在我将尝试创建一个泛型方法并使用实例化类型,但我认为这不起作用,因为反射实例化会创建一个类型为object的实例。表达树?希望我只是碰到了我的盲点,并且在这么晚的时候我会想到类似的东西。

0 个答案:

没有答案