public override ClassLayoutMetadata GetClassLayout() { ClassLayoutMetadata layout = ManagedStructType.GetClassLayout(); ClassLayoutMetadata result; result.PackingSize = layout.PackingSize; result.Size = layout.Size; if (IsExplicitLayout) { result.Offsets = new FieldAndOffset[layout.Offsets.Length]; Debug.Assert(layout.Offsets.Length <= _fields.Length); int layoutIndex = 0; for (int index = 0; index < _fields.Length; index++) { if (_fields[index].Name == layout.Offsets[layoutIndex].Field.Name) { result.Offsets[layoutIndex] = new FieldAndOffset(_fields[index], layout.Offsets[layoutIndex].Offset); layoutIndex++; } } Debug.Assert(layoutIndex == layout.Offsets.Length); } else { result.Offsets = null; } return(result); }
private void CalculateFields() { bool isSequential = ManagedStructType.IsSequentialLayout; MarshalAsDescriptor[] marshalAsDescriptors = ManagedStructType.GetFieldMarshalAsDescriptors(); bool isAnsi = ((MetadataType)ManagedStructType).PInvokeStringFormat == PInvokeStringFormat.AnsiClass; int numFields = 0; foreach (FieldDesc field in ManagedStructType.GetFields()) { if (field.IsStatic) { continue; } numFields++; } _fields = new NativeStructField[numFields]; int index = 0; foreach (FieldDesc field in ManagedStructType.GetFields()) { if (field.IsStatic) { continue; } var managedType = field.FieldType; TypeDesc nativeType; try { nativeType = MarshalHelpers.GetNativeStructFieldType(managedType, marshalAsDescriptors[index], _interopStateManager, isAnsi); } catch (NotSupportedException) { // if marshalling is not supported for this type the generated stubs will emit appropriate // error message. We just set native type to be same as managedtype nativeType = managedType; _hasInvalidLayout = true; } _fields[index++] = new NativeStructField(nativeType, this, field); } }