Exemplo n.º 1
0
            private void PopulateInheritance(MetadataDefinition metadataTypeDefinition)
            {
                var derivedTypeDefinition = metadataTypeDefinition.Type;
                var interfaceImplHandles  = derivedTypeDefinition.GetInterfaceImplementations();

                if (derivedTypeDefinition.BaseType.IsNil && interfaceImplHandles.Count == 0)
                {
                    return;
                }

                var derivedTypeSimpleName = metadataTypeDefinition.Name;

                PopulateInheritance(derivedTypeSimpleName, derivedTypeDefinition.BaseType);

                foreach (var interfaceImplHandle in interfaceImplHandles)
                {
                    if (!interfaceImplHandle.IsNil)
                    {
                        var interfaceImpl = _metadataReader.GetInterfaceImplementation(
                            interfaceImplHandle
                            );
                        PopulateInheritance(derivedTypeSimpleName, interfaceImpl.Interface);
                    }
                }
            }
Exemplo n.º 2
0
        private DiscoveredType(MetadataReader reader, TypeDefinitionHandle handle)
        {
            var definition = reader.GetTypeDefinition(handle);

            this.Token      = reader.GetToken(handle);
            this.Name       = reader.GetString(definition.Name);
            this.Namespace  = reader.GetString(definition.Namespace);
            this.Interfaces = definition.GetInterfaceImplementations()
                              .Select(i => reader.GetInterfaceImplementation(i).Interface)
                              .Select(i => DiscoveredType.FromInterface(reader.GetToken(i)));
            this.isLocal = true;
        }
        /// <summary>
        /// Gets a collection of <see cref="TypeDescriptor"/>s for each implemented interface for the
        /// given <paramref name="type"/>.
        /// </summary>
        /// <param name="reader"> The extended <see cref="MetadataReader"/>. </param>
        /// <param name="type"> The type whose implemented interfaces should be returned. </param>
        /// <returns> A collection of <see cref="TypeDescriptor"/>s for each implemented interface. </returns>
        public static IEnumerable <TypeDescriptor> GetImplementedInterfaces(this MetadataReader reader, TypeDefinition type)
        {
            var interfaceImplementations = type.GetInterfaceImplementations();

            if (interfaceImplementations.Count == 0)
            {
                return(Enumerable.Empty <TypeDescriptor>());
            }

            var implementedInterfaces = new List <TypeDescriptor>();

            foreach (var implementationHandle in interfaceImplementations)
            {
                var interfaceImplementation = reader.GetInterfaceImplementation(implementationHandle);

                // interface is declared in the same assembly as the type
                if (interfaceImplementation.Interface.Kind == HandleKind.TypeDefinition)
                {
                    var definition = reader.GetTypeDefinition((TypeDefinitionHandle)interfaceImplementation.Interface);
                    var @interface = reader.ToTypeDescriptor(definition);
                    implementedInterfaces.Add(@interface);
                }
                // interface is declared in another assembly
                else if (interfaceImplementation.Interface.Kind == HandleKind.TypeReference)
                {
                    var reference  = reader.GetTypeReference((TypeReferenceHandle)interfaceImplementation.Interface);
                    var @interface = reader.ToTypeDescriptor(reference);
                    implementedInterfaces.Add(@interface);
                }
                // interface is generic
                if (interfaceImplementation.Interface.Kind == HandleKind.TypeSpecification)
                {
                    var specification = reader.GetTypeSpecification((TypeSpecificationHandle)interfaceImplementation.Interface);
                    var @interface    = specification.DecodeSignature(new TypeDescriptorSignatureProvider(), null);
                    implementedInterfaces.Add(@interface);
                }
            }
            return(implementedInterfaces);
        }
 private static void Main(string[] args)
 {
     foreach (string fileName in new string[] { @"C:\git\corert\bin\Windows_NT.x64.Debug\ILVerification.Tests\Tests\InterfaceImplementation.dll" })
     {
         Console.WriteLine("Lib: " + fileName);
         PEReader       peReader       = new PEReader(File.OpenRead(fileName));
         MetadataReader metadataReader = peReader.GetMetadataReader(MetadataReaderOptions.None);
         Console.WriteLine("IsAssembly " + metadataReader.IsAssembly);
         foreach (TypeDefinitionHandle td in metadataReader.TypeDefinitions)
         {
             TypeDefinition typeDefinition = metadataReader.GetTypeDefinition(td);
             if (metadataReader.GetString(typeDefinition.Name) != "InvalidReturnTypeM1_InvalidType_InterfaceMethodNotImplemented")
             {
                 continue;
             }
             Console.WriteLine("TypeDefHandle token:" + metadataReader.GetToken(td).ToString("X8"));
             Console.WriteLine("TypeDefinition name: " + metadataReader.GetString(typeDefinition.Name) + ((((int)typeDefinition.Attributes & 0x00000020) == 0x00000020) ? " [IsInterface]" : ""));
             Console.WriteLine("Attributes name: " + typeDefinition.Attributes);
             foreach (InterfaceImplementationHandle interfaceImplementation in typeDefinition.GetInterfaceImplementations())
             {
                 InterfaceImplementation im = metadataReader.GetInterfaceImplementation(interfaceImplementation);
                 Console.WriteLine("InterfaceImplementationHandle:" + metadataReader.GetToken(im.Interface).ToString("X8"));
             }
             foreach (MethodDefinitionHandle mdh in typeDefinition.GetMethods())
             {
                 MethodDefinition md = metadataReader.GetMethodDefinition(mdh);
                 Console.WriteLine(" Method name: " + metadataReader.GetString(md.Name));
                 foreach (ParameterHandle ph in md.GetParameters())
                 {
                     Parameter parameterDef = metadataReader.GetParameter(ph);
                     Console.WriteLine("     Param name: " + metadataReader.GetString(parameterDef.Name) + (parameterDef.SequenceNumber == 0 ? "- return value" : ""));
                 }
                 Console.WriteLine("MethodDefinitionHandle:" + metadataReader.GetToken(mdh).ToString("X8"));
             }
             Console.WriteLine("----");
         }
     }
 }
Exemplo n.º 5
0
 public static string ToString(this MetadataReader reader, InterfaceImplementationHandle x) => reader.ToString(reader.GetInterfaceImplementation(x));
Exemplo n.º 6
0
 public static InterfaceImplementation GetInterfaceImplementation(this InterfaceImplementationHandle handle, MetadataReader reader) => reader.GetInterfaceImplementation(handle);
        private static List <IType> LoadDefinedTypes(MetadataReader reader, IAssembly assembly)
        {
            var definitions = from handle
                              in reader.TypeDefinitions
                              let definition = reader.GetTypeDefinition(handle)
                                               let interfaces = from theInterface
                                                                in definition.GetInterfaceImplementations()
                                                                let theImplementation = reader.GetInterfaceImplementation(theInterface).Interface
                                                                                        select new ScannedType(reader.GetToken(theImplementation)).AsInterface()
                                                                                        select new ScannedType(
                token: reader.GetToken(handle),
                isLocal: true,
                name: reader.GetString(definition.Name),
                theNamespace: reader.GetString(definition.Namespace),
                assembly: assembly,
                baseType: new ScannedType(reader.GetToken(definition.BaseType)),
                attributes: definition.Attributes,
                interfaces: interfaces.ToList()).AsInterface();

            return(definitions.ToList());
        }