Inheritance: TypeSignature, IGenericArgumentsProvider
Beispiel #1
0
        private static TypeSignature ReadTypeSignature(
            MetadataImage image,
            IBinaryStreamReader reader,
            RecursionProtection protection)
        {
            var elementType = (ElementType)reader.ReadByte();

            switch (elementType)
            {
            case ElementType.Array:
                return(ArrayTypeSignature.FromReader(image, reader, protection));

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

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

            case ElementType.CModOpt:
                return(OptionalModifierSignature.FromReader(image, reader, protection));

            case ElementType.CModReqD:
                return(RequiredModifierSignature.FromReader(image, reader, protection));

            case ElementType.Class:
                return(TypeDefOrRefSignature.FromReader(image, reader, protection));

            case ElementType.FnPtr:
                return(FunctionPointerTypeSignature.FromReader(image, reader, protection));

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

            case ElementType.MVar:
                return(GenericParameterSignature.FromReader(image, reader, GenericParameterType.Method));

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

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

            case ElementType.Sentinel:
                return(SentinelTypeSignature.FromReader(image, reader, protection));

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

            case ElementType.ValueType:
                var type = TypeDefOrRefSignature.FromReader(image, reader, protection);
                type.IsValueType = true;
                return(type);

            case ElementType.Var:
                return(GenericParameterSignature.FromReader(image, reader, GenericParameterType.Type));

            default:
                return(MsCorLibTypeSignature.FromElementType(image, elementType));
            }
        }
        public static new GenericInstanceTypeSignature FromReader(MetadataHeader header, IBinaryStreamReader reader)
        {
            if (!reader.CanRead(sizeof (byte)))
                return null;

            long position = reader.Position;

            var elementType = (ElementType)reader.ReadByte();

            var type = ReadTypeDefOrRef(header, reader);

            var signature = new GenericInstanceTypeSignature(type)
            {
                StartOffset = position,
                IsValueType = elementType == ElementType.ValueType
            };

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

            for (int i = 0; i < count; i++)
                signature.GenericArguments.Add(TypeSignature.FromReader(header, reader));

            return signature;
        }
Beispiel #3
0
        /// <summary>
        /// Reads a single generic instance type signature at the current position of the provided stream reader.
        /// </summary>
        /// <param name="image">The image the signature was defined in.</param>
        /// <param name="reader">The reader to use.</param>
        /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param>
        /// <returns>The read signature.</returns>
        public static GenericInstanceTypeSignature FromReader(
            MetadataImage image,
            IBinaryStreamReader reader,
            RecursionProtection protection)
        {
            if (!reader.CanRead(sizeof(byte)))
            {
                return(null);
            }

            var elementType = (ElementType)reader.ReadByte();
            var type        = ReadTypeDefOrRef(image, reader, protection);

            var signature = new GenericInstanceTypeSignature(type)
            {
                IsValueType = elementType == ElementType.ValueType
            };

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

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

            return(signature);
        }
        public new static GenericInstanceTypeSignature FromReader(MetadataHeader header, IBinaryStreamReader reader)
        {
            if (!reader.CanRead(sizeof(byte)))
            {
                return(null);
            }

            var elementType = (ElementType)reader.ReadByte();

            var type = ReadTypeDefOrRef(header, reader);

            var signature = new GenericInstanceTypeSignature(type)
            {
                IsValueType = elementType == ElementType.ValueType
            };

            uint count;

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

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

            return(signature);
        }
Beispiel #5
0
        public static TypeSignature FromReader(MetadataHeader header, IBinaryStreamReader reader)
        {
            var elementType = (ElementType)reader.ReadByte();

            switch (elementType)
            {
            case ElementType.Array:
                return(ArrayTypeSignature.FromReader(header, reader));

            case ElementType.Boxed:
                return(BoxedTypeSignature.FromReader(header, reader));

            case ElementType.ByRef:
                return(ByReferenceTypeSignature.FromReader(header, reader));

            case ElementType.CModOpt:
                return(OptionalModifierSignature.FromReader(header, reader));

            case ElementType.CModReqD:
                return(RequiredModifierSignature.FromReader(header, reader));

            case ElementType.Class:
                return(TypeDefOrRefSignature.FromReader(header, reader));

            case ElementType.FnPtr:
                return(FunctionPointerTypeSignature.FromReader(header, reader));

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

            case ElementType.MVar:
                return(GenericParameterSignature.FromReader(header, reader, GenericParameterType.Method));

            case ElementType.Pinned:
                return(PinnedTypeSignature.FromReader(header, reader));

            case ElementType.Ptr:
                return(PointerTypeSignature.FromReader(header, reader));

            case ElementType.Sentinel:
                return(SentinelTypeSignature.FromReader(header, reader));

            case ElementType.SzArray:
                return(SzArrayTypeSignature.FromReader(header, reader));

            case ElementType.ValueType:
                var type = TypeDefOrRefSignature.FromReader(header, reader);
                type.IsValueType = true;
                return(type);

            case ElementType.Var:
                return(GenericParameterSignature.FromReader(header, reader, GenericParameterType.Type));

            default:
                return(MsCorLibTypeSignature.FromElementType(header, elementType));
            }
            throw new NotSupportedException();
        }
        public override TypeSignature InstantiateGenericTypes(IGenericContext context)
        {
            var result = new GenericInstanceTypeSignature(GenericType)
            {
                IsValueType = IsValueType
            };

            foreach (var argument in GenericArguments)
            {
                result.GenericArguments.Add(argument.InstantiateGenericTypes(context));
            }
            return(result);
        }
        public void CreateGenericInstanceField()
        {
            // set up temp assembly.
            var assembly = Utilities.CreateTempNetAssembly();
            var typeSystem = assembly.NetDirectory.MetadataHeader.TypeSystem;
            var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>();
            var fieldTable = tableStream.GetTable<FieldDefinition>();
            var importer = new ReferenceImporter(tableStream);

            // create field.
            var typeSignature = new GenericInstanceTypeSignature(importer.ImportType(typeof(List<>)));
            typeSignature.GenericArguments.Add(typeSystem.String);
            var field = new FieldDefinition(FieldName, FieldAttributes.Public | FieldAttributes.Static,
                new FieldSignature(typeSignature));
            fieldTable.Add(field);

            // build and validate.
            assembly = Utilities.RebuildNetAssembly(assembly);
            fieldTable = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>().GetTable<FieldDefinition>();
            field = fieldTable.First(x => x.Name == FieldName);

            Assert.IsInstanceOfType(field.Signature.FieldType, typeof(GenericInstanceTypeSignature));
            var newTypeSignature = (GenericInstanceTypeSignature)field.Signature.FieldType;
            Utilities.ValidateType(typeSignature.GenericType, newTypeSignature.GenericType);
            Assert.AreEqual(typeSignature.GenericArguments.Count, newTypeSignature.GenericArguments.Count);
            for (int i = 0; i < typeSignature.GenericArguments.Count; i++)
                Assert.AreEqual(typeSignature.GenericArguments[i].FullName,
                    newTypeSignature.GenericArguments[i].FullName);
        }
 private GenericInstanceTypeSignature ImportGenericInstanceTypeSignature(GenericInstanceTypeSignature signature)
 {
     var newSignature = new GenericInstanceTypeSignature(ImportType(signature.GenericType))
     {
         IsValueType = signature.IsValueType
     };
     foreach (var argument in signature.GenericArguments)
         newSignature.GenericArguments.Add(ImportTypeSignature(argument));
     return newSignature;
 }
 private GenericInstanceTypeSignature ImportGenericInstanceTypeSignature(Type type)
 {
     var signature = new GenericInstanceTypeSignature(ImportType(type))
     {
         IsValueType = type.IsValueType
     };
     foreach (var argument in type.GetGenericArguments())
         signature.GenericArguments.Add(ImportTypeSignature(argument));
     return signature;
 }
Beispiel #10
0
        /// <summary>
        /// Determines whether two types are considered equal according to their signature.
        /// </summary>
        /// <param name="signature1">The first type to compare.</param>
        /// <param name="signature2">The second type to compare.</param>
        /// <returns><c>True</c> if the types are considered equal, <c>False</c> otherwise.</returns>
        public bool MatchTypes(GenericInstanceTypeSignature signature1, GenericInstanceTypeSignature signature2)
        {
            if (signature1 == null && signature2 == null)
                return true;
            if (signature1 == null || signature2 == null)
                return false;

            return MatchTypes(signature1.GenericType, signature2.GenericType)
                && MatchManyTypes(signature1.GenericArguments, signature2.GenericArguments);
        }
        public void MatchGenericTypeSignatures()
        {
            var expected = new GenericInstanceTypeSignature(CreateTypeRef1());
            expected.GenericArguments.Add(CreateTypeSig3());
            var match = new GenericInstanceTypeSignature(CreateTypeRef1());
            match.GenericArguments.Add(CreateTypeSig3());
            var fail1 = new GenericInstanceTypeSignature(CreateTypeRef2());
            fail1.GenericArguments.Add(CreateTypeSig1());
            var fail2 = new GenericInstanceTypeSignature(CreateTypeRef1());
            fail2.GenericArguments.Add(CreateTypeSig2());

            VerifyMatching(expected, match, fail1, fail2);
        }