【发布时间】:2021-07-06 08:06:29
【问题描述】:
这是结构:
public struct TargetStruct : SomeInterface
{
private RigidTransform rt;
public RoundBounds rb;
public int Start;
public Bool IsEnabled;
public TypeEnum TheType;
}
//
// Nested types
//
public struct RoundBounds : SomeInterface
{
public float3 Center;
public float Radius;
public float RadiusSq;
}
public struct Bool : IEquatable<Bool>, SomeInterface
{
[MarshalAs(UnmanagedType.I1)]
private bool value;
}
public enum TypeEnum : byte
{
None,
Type1,
Type1
}
public struct RigidTransform
{
public quaternion rot;
public float3 pos;
public static readonly RigidTransform identity = new RigidTransform(new quaternion(0f, 0f, 0f, 1f), new float3(0f, 0f, 0f));
}
这在 Unity 的单声道2019.4.12-mbe 下运行,如果相关,Unity 会生成面向 4.7.1 的项目。结构正在通过具有约束where T: struct 的泛型方法传递给 SizeOf,尽管这应该无关紧要。
这里是 mono 的 mscorlib Marshal.SizeOf<T> 的 IL:
呼叫转至外部SizeOf(Type t);
什么可能导致堆分配?我唯一的假设是拳击,但我在这里看不到任何拳击。 GetType()should not allocate
【问题讨论】:
标签: c# unity3d mono marshalling unmanaged-memory