示例#1
0
        public void Add <T>(string key, ref T v)
        {
            var e = new StructElement
            {
                name   = Marshal.StringToHGlobalAnsi(key),
                type   = TypeExtensions.GetTypeCode(typeof(T)),
                offset = (short)size,
                size   = (short)TypeExtensions.GetSize(typeof(T)),
            };

            Add(e);
        }
示例#2
0
        public static StructElement[] GetLayout(this Type type, out int size)
        {
            if (!type.IsUnManaged())
            {
                size = 0;
                return(null);
            }

            var fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            int len        = fieldInfos.Length;

            StructElement[] layout = new StructElement[len];
            size = 0;
            for (int i = 0; i < len; i++)
            {
                var     fieldInfo = fieldInfos[i];
                ref var e         = ref layout[i];
                e.name   = Marshal.StringToHGlobalAnsi(fieldInfo.Name);
                e.type   = GetTypeCode(fieldInfo.FieldType);
                e.offset = (short)Marshal.OffsetOf(type, fieldInfo.Name);
                e.size   = (short)GetSize(fieldInfo.FieldType);
                Debug.Assert(e.size != 0);
                size += e.size;
            }