diff --git a/src/DotNet/Importer.cs b/src/DotNet/Importer.cs index de37e66b..ab4f424a 100644 --- a/src/DotNet/Importer.cs +++ b/src/DotNet/Importer.cs @@ -225,8 +225,14 @@ TypeSig ImportAsTypeSig(Type type, Type declaringType, bool? treatAsGenericInst case ElementType.SZArray: return new SZArraySig(ImportAsTypeSig(type.GetElementType(), declaringType)); case ElementType.ValueType: return new ValueTypeSig(CreateTypeDefOrRef(type)); case ElementType.Class: return new ClassSig(CreateTypeDefOrRef(type)); - case ElementType.Var: return new GenericVar((uint)type.GenericParameterPosition, gpContext.Type); - case ElementType.MVar: return new GenericMVar((uint)type.GenericParameterPosition, gpContext.Method); + case ElementType.Var: + if (gpContext.Type is null && IsThisModule(type.Module) && module.ResolveToken(type.DeclaringType.MetadataToken) is TypeDef ownerType) + return new GenericVar((uint)type.GenericParameterPosition, ownerType); + return new GenericVar((uint)type.GenericParameterPosition, gpContext.Type); + case ElementType.MVar: + if (gpContext.Method is null && IsThisModule(type.Module) && module.ResolveToken(type.DeclaringMethod.MetadataToken) is MethodDef ownerMethod) + return new GenericMVar((uint)type.GenericParameterPosition, ownerMethod); + return new GenericMVar((uint)type.GenericParameterPosition, gpContext.Method); case ElementType.I: FixSignature = true; // FnPtr is mapped to System.IntPtr @@ -827,11 +833,11 @@ public TypeSig Import(TypeSig type) { case ElementType.ByRef: result = new ByRefSig(Import(type.Next)); break; case ElementType.ValueType: result = CreateClassOrValueType((type as ClassOrValueTypeSig).TypeDefOrRef, true); break; case ElementType.Class: result = CreateClassOrValueType((type as ClassOrValueTypeSig).TypeDefOrRef, false); break; - case ElementType.Var: result = new GenericVar((type as GenericVar).Number, gpContext.Type); break; + case ElementType.Var: result = new GenericVar((type as GenericVar).Number, gpContext.Type ?? (type as GenericVar).OwnerType); break; case ElementType.ValueArray:result = new ValueArraySig(Import(type.Next), (type as ValueArraySig).Size); break; case ElementType.FnPtr: result = new FnPtrSig(Import((type as FnPtrSig).Signature)); break; case ElementType.SZArray: result = new SZArraySig(Import(type.Next)); break; - case ElementType.MVar: result = new GenericMVar((type as GenericMVar).Number, gpContext.Method); break; + case ElementType.MVar: result = new GenericMVar((type as GenericMVar).Number, gpContext.Method ?? (type as GenericVar).OwnerMethod); break; case ElementType.CModReqd: result = new CModReqdSig(Import((type as ModifierSig).Modifier), Import(type.Next)); break; case ElementType.CModOpt: result = new CModOptSig(Import((type as ModifierSig).Modifier), Import(type.Next)); break; case ElementType.Module: result = new ModuleSig((type as ModuleSig).Index, Import(type.Next)); break;