/// <summary> /// Generate the specified union /// </summary> /// <param name="ntUnion"></param> /// <returns></returns> /// <remarks></remarks> public CodeTypeDeclaration GenerateUnion(NativeUnion ntUnion) { if (ntUnion == null) { throw new ArgumentNullException("ntUnion"); } // Generate the core type CodeTypeDeclaration ctd = new CodeTypeDeclaration(ntUnion.Name); ctd.IsStruct = true; ctd.UserData[TransformConstants.DefinedType] = ntUnion; // Add the struct layout attribute ctd.CustomAttributes.Add(MarshalAttributeFactory.CreateStructLayoutAttribute(LayoutKind.Explicit)); // Generate the container members GenerateContainerMembers(ntUnion, ctd); // Go through and put each struct back at the start of the struct to simulate the // union foreach (CodeTypeMember member in ctd.Members) { CodeMemberField fieldMember = member as CodeMemberField; if (fieldMember != null) { fieldMember.CustomAttributes.Add(MarshalAttributeFactory.CreateFieldOffsetAttribute(0)); } } return(ctd); }
/// <summary> /// Generate the struct definition /// </summary> /// <param name="ntStruct"></param> /// <returns></returns> /// <remarks></remarks> public CodeTypeDeclaration GenerateStruct(NativeStruct ntStruct) { if (ntStruct == null) { throw new ArgumentNullException("ntStruct"); } // Generate the core type CodeTypeDeclaration ctd = new CodeTypeDeclaration(ntStruct.Name); ctd.IsStruct = true; ctd.UserData[TransformConstants.DefinedType] = ntStruct; // Add the struct layout attribute ctd.CustomAttributes.Add(MarshalAttributeFactory.CreateStructLayoutAttribute(LayoutKind.Sequential)); GenerateContainerMembers(ntStruct, ctd); return(ctd); }