コード例 #1
0
        internal new static GenericInstanceTypeSignature FromReader(ModuleDefinition module, IBinaryStreamReader reader,
                                                                    RecursionProtection protection)
        {
            var genericType = TypeSignature.FromReader(module, reader, protection);
            var signature   = new GenericInstanceTypeSignature(genericType.ToTypeDefOrRef(), genericType.ElementType == ElementType.ValueType);

            if (!reader.TryReadCompressedUInt32(out uint count))
            {
                return(signature);
            }

            for (int i = 0; i < count; i++)
            {
                signature.TypeArguments.Add(TypeSignature.FromReader(module, reader, protection));
            }

            return(signature);
        }
コード例 #2
0
        /// <summary>
        /// Reads a type signature from a blob reader.
        /// </summary>
        /// <param name="module">The module containing the blob signature.</param>
        /// <param name="reader">The blob signature reader.</param>
        /// <param name="protection">The object responsible for detecting infinite recursion.</param>
        /// <returns>The type signature.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Occurs when the blob reader points to an element type that is
        /// invalid or unsupported.</exception>
        public static TypeSignature FromReader(ModuleDefinition module, IBinaryStreamReader reader,
                                               RecursionProtection protection)
        {
            var elementType = (ElementType)reader.ReadByte();

            switch (elementType)
            {
            case ElementType.Void:
            case ElementType.Boolean:
            case ElementType.Char:
            case ElementType.I1:
            case ElementType.U1:
            case ElementType.I2:
            case ElementType.U2:
            case ElementType.I4:
            case ElementType.U4:
            case ElementType.I8:
            case ElementType.U8:
            case ElementType.R4:
            case ElementType.R8:
            case ElementType.String:
            case ElementType.I:
            case ElementType.U:
            case ElementType.TypedByRef:
            case ElementType.Object:
                return(module.CorLibTypeFactory.FromElementType(elementType));

            case ElementType.ValueType:
                return(new TypeDefOrRefSignature(ReadTypeDefOrRef(module, reader, protection, false), true));

            case ElementType.Class:
                return(new TypeDefOrRefSignature(ReadTypeDefOrRef(module, reader, protection, false), false));

            case ElementType.Ptr:
                return(new PointerTypeSignature(FromReader(module, reader, protection)));

            case ElementType.ByRef:
                return(new ByReferenceTypeSignature(FromReader(module, reader, protection)));

            case ElementType.Var:
                return(new GenericParameterSignature(module, GenericParameterType.Type,
                                                     (int)reader.ReadCompressedUInt32()));

            case ElementType.MVar:
                return(new GenericParameterSignature(module, GenericParameterType.Method,
                                                     (int)reader.ReadCompressedUInt32()));

            case ElementType.Array:
                return(ArrayTypeSignature.FromReader(module, reader, protection));

            case ElementType.GenericInst:
                return(GenericInstanceTypeSignature.FromReader(module, reader, protection));

            case ElementType.FnPtr:
                throw new NotImplementedException();

            case ElementType.SzArray:
                return(new SzArrayTypeSignature(FromReader(module, reader, protection)));

            case ElementType.CModReqD:
                return(new CustomModifierTypeSignature(
                           ReadTypeDefOrRef(module, reader, protection, true),
                           true,
                           FromReader(module, reader, protection)));

            case ElementType.CModOpt:
                return(new CustomModifierTypeSignature(
                           ReadTypeDefOrRef(module, reader, protection, true),
                           false,
                           FromReader(module, reader, protection)));

            case ElementType.Sentinel:
                return(new SentinelTypeSignature());

            case ElementType.Pinned:
                return(new PinnedTypeSignature(FromReader(module, reader, protection)));

            case ElementType.Boxed:
                return(new BoxedTypeSignature(FromReader(module, reader, protection)));

            case ElementType.Internal:
                IntPtr address = IntPtr.Size switch
                {
                    4 => new IntPtr(reader.ReadInt32()),
                    _ => new IntPtr(reader.ReadInt64())
                };

                //Get Internal Method Through Reflection
                var GetTypeFromHandleUnsafeReflection = typeof(Type)
                                                        .GetMethod("GetTypeFromHandleUnsafe", ((BindingFlags)(-1)), null, new[] { typeof(IntPtr) },
                                                                   null);
                //Invoke It To Get The Value
                var Type = (Type)GetTypeFromHandleUnsafeReflection?.Invoke(null, new object[] { address });
                //Import it
                return(new TypeDefOrRefSignature(new ReferenceImporter(module).ImportType((Type))));

            default:
                throw new ArgumentOutOfRangeException($"Invalid or unsupported element type {elementType}.");
            }
        }