Exemplo n.º 1
0
        /// <summary>
        /// Reads a marshal descriptor signature from the provided input stream.
        /// </summary>
        /// <param name="parentModule">The module that defines the marshal descriptor</param>
        /// <param name="reader">The input stream.</param>
        /// <returns>The marshal descriptor.</returns>
        public static MarshalDescriptor FromReader(ModuleDefinition parentModule, ref BinaryStreamReader reader)
        {
            var nativeType = (NativeType)reader.ReadByte();
            MarshalDescriptor descriptor = nativeType switch
            {
                NativeType.SafeArray => SafeArrayMarshalDescriptor.FromReader(parentModule, ref reader),
                NativeType.FixedArray => FixedArrayMarshalDescriptor.FromReader(ref reader),
                NativeType.LPArray => LPArrayMarshalDescriptor.FromReader(ref reader),
                NativeType.CustomMarshaller => CustomMarshalDescriptor.FromReader(parentModule, ref reader),
                NativeType.FixedSysString => FixedSysStringMarshalDescriptor.FromReader(ref reader),
                NativeType.Interface => ComInterfaceMarshalDescriptor.FromReader(nativeType, ref reader),
                NativeType.IDispatch => ComInterfaceMarshalDescriptor.FromReader(nativeType, ref reader),
                NativeType.IUnknown => ComInterfaceMarshalDescriptor.FromReader(nativeType, ref reader),
                _ => new SimpleMarshalDescriptor(nativeType)
            };

            descriptor.ExtraData = reader.ReadToEnd();
            return(descriptor);
        }
        /// <summary>
        /// Reads a single safe array marshal descriptor from the provided input stream.
        /// </summary>
        /// <param name="parentModule">The module defining the descriptor.</param>
        /// <param name="reader">The input stream.</param>
        /// <returns>The descriptor.</returns>
        public new static SafeArrayMarshalDescriptor FromReader(ModuleDefinition parentModule, IBinaryStreamReader reader)
        {
            if (!reader.TryReadCompressedUInt32(out uint type))
            {
                return(new SafeArrayMarshalDescriptor(SafeArrayVariantType.NotSet));
            }

            var variantType = (SafeArrayVariantType)type & SafeArrayVariantType.TypeMask;
            var flags       = (SafeArrayTypeFlags)type & ~SafeArrayTypeFlags.Mask;

            var result = new SafeArrayMarshalDescriptor(variantType, flags);

            if (reader.CanRead(1))
            {
                string typeName = reader.ReadSerString();
                if (typeName != null)
                {
                    result.UserDefinedSubType = TypeNameParser.Parse(parentModule, typeName);
                }
            }

            return(result);
        }