コード例 #1
0
        /// <inheritdoc/>
        public TypeDef Resolve(TypeRef typeRef, ModuleDef sourceModule)
        {
            if (typeRef == null)
            {
                return(null);
            }

            var nonNestedTypeRef = TypeRef.GetNonNestedTypeRef(typeRef);

            if (nonNestedTypeRef == null)
            {
                return(null);
            }

            var nonNestedResolutionScope = nonNestedTypeRef.ResolutionScope;
            var nonNestedModule          = nonNestedTypeRef.Module;
            var asmRef = nonNestedResolutionScope as AssemblyRef;

            if (asmRef != null)
            {
                var asm = assemblyResolver.Resolve(asmRef, sourceModule ?? nonNestedModule);
                return(asm == null ? null : asm.Find(typeRef) ?? ResolveExportedType(asm.Modules, typeRef, sourceModule));
            }

            var moduleDef = nonNestedResolutionScope as ModuleDef;

            if (moduleDef != null)
            {
                return(moduleDef.Find(typeRef) ??
                       ResolveExportedType(new ModuleDef[] { moduleDef }, typeRef, sourceModule));
            }

            var moduleRef = nonNestedResolutionScope as ModuleRef;

            if (moduleRef != null)
            {
                if (nonNestedModule == null)
                {
                    return(null);
                }
                if (new SigComparer().Equals(moduleRef, nonNestedModule))
                {
                    return(nonNestedModule.Find(typeRef) ??
                           ResolveExportedType(new ModuleDef[] { nonNestedModule }, typeRef, sourceModule));
                }
                var nonNestedAssembly = nonNestedModule.Assembly;
                if (nonNestedAssembly == null)
                {
                    return(null);
                }
                var resolvedModule = nonNestedAssembly.FindModule(moduleRef.Name);
                return(resolvedModule == null ? null : resolvedModule.Find(typeRef) ??
                       ResolveExportedType(new ModuleDef[] { resolvedModule }, typeRef, sourceModule));
            }

            return(null);
        }
コード例 #2
0
        private TypeSig ReadType(bool readAssemblyReference)
        {
            RecursionIncrement();
            TypeSig result;

            SkipWhite();
            if (PeekChar() == '!')
            {
                var currentSig = ReadGenericSig();
                var tspecs     = ReadTSpecs();
                ReadOptionalAssemblyRef();
                result = CreateTypeSig(tspecs, currentSig);
            }
            else
            {
                var         typeRef          = ReadTypeRefAndNestedNoAssembly('+');
                var         tspecs           = ReadTSpecs();
                var         nonNestedTypeRef = TypeRef.GetNonNestedTypeRef(typeRef);
                AssemblyRef asmRef;
                if (readAssemblyReference)
                {
                    asmRef = ReadOptionalAssemblyRef() ?? FindAssemblyRef(nonNestedTypeRef);
                }
                else
                {
                    asmRef = FindAssemblyRef(nonNestedTypeRef);
                }
                nonNestedTypeRef.ResolutionScope = asmRef;

                // Make sure the CorLib types are used whenever possible
                result = null;
                if (typeRef == nonNestedTypeRef)
                {
                    var corLibSig = ownerModule.CorLibTypes.GetCorLibTypeSig(typeRef.Namespace, typeRef.Name, typeRef.DefinitionAssembly);
                    if (corLibSig != null)
                    {
                        result = corLibSig;
                    }
                }
                if (result == null)
                {
                    var typeDef = Resolve(asmRef, typeRef);
                    result = ToTypeSig(typeDef != null ? (ITypeDefOrRef)typeDef : typeRef);
                }

                if (tspecs.Count != 0)
                {
                    result = CreateTypeSig(tspecs, result);
                }
            }

            RecursionDecrement();
            return(result);
        }