Exemple #1
0
        /// <inheritdoc />
        public TypeDefinition ResolveType(ITypeDescriptor type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            type = type.GetElementType();

            if (type is TypeDefinition typeDef)
            {
                return(typeDef);
            }

            var assemblyDescriptor = type.ResolutionScope.GetAssembly();

            if (assemblyDescriptor == null)
            {
                return((TypeDefinition)ThrowOrReturn(type));
            }

            var assembly = assemblyDescriptor as AssemblyDefinition;

            if (assembly == null)
            {
                assembly = AssemblyResolver.ResolveAssembly(assemblyDescriptor);
            }
            if (assembly == null)
            {
                return((TypeDefinition)ThrowOrReturn(type));
            }

            IEnumerable <TypeDefinition> types;

            if (type.DeclaringTypeDescriptor != null)
            {
                var declaringType = ResolveType(type.DeclaringTypeDescriptor);
                if (declaringType == null)
                {
                    return((TypeDefinition)ThrowOrReturn(type));
                }
                types = declaringType.NestedClasses.Select(x => x.Class);
            }
            else
            {
                types = assembly.Modules[0].TopLevelTypes;
            }
            var definition = types.FirstOrDefault(x => _signatureComparer.Equals(x, type));

            return(definition ?? (TypeDefinition)ThrowOrReturn(type));
        }
        public TypeDefinition ResolveType(ITypeDescriptor type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            type = type.GetElementType();

            var typeDef = type as TypeDefinition;

            if (typeDef != null)
            {
                return(typeDef);
            }

            var assemblyDescriptor = type.ResolutionScope.GetAssembly();

            if (assemblyDescriptor == null)
            {
                return((TypeDefinition)ThrowOrReturn(type));
            }

            var assembly = AssemblyResolver.ResolveAssembly(assemblyDescriptor);

            if (assembly == null)
            {
                return((TypeDefinition)ThrowOrReturn(type));
            }

            var typeDefTable = assembly.Header.GetStream <TableStream>().GetTable <TypeDefinition>();

            if (typeDefTable == null)
            {
                return((TypeDefinition)ThrowOrReturn(type));
            }

            var definition = typeDefTable.FirstOrDefault(x => _signatureComparer.MatchTypes(x, type));

            return(definition ?? (TypeDefinition)ThrowOrReturn(type));
        }
        public TypeDefinition ResolveType(ITypeDescriptor type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            type = type.GetElementType();

            var typeDef = type as TypeDefinition;
            if (typeDef != null)
                return typeDef;

            var assemblyDescriptor = type.ResolutionScope.GetAssembly();
            if (assemblyDescriptor == null)
                return (TypeDefinition)ThrowOrReturn(type);

            var assembly = AssemblyResolver.ResolveAssembly(assemblyDescriptor);
            if (assembly == null)
                return (TypeDefinition)ThrowOrReturn(type);

            var typeDefTable = assembly.Header.GetStream<TableStream>().GetTable<TypeDefinition>();
            if (typeDefTable == null)
                return (TypeDefinition)ThrowOrReturn(type);

            var definition = typeDefTable.FirstOrDefault(x => _signatureComparer.MatchTypes(x, type));
            return definition ?? (TypeDefinition)ThrowOrReturn(type);
        }