diff --git a/dnSpy/Roslyn/dnSpy.Roslyn/Debugger/GetLocalsAssemblyBuilder.cs b/dnSpy/Roslyn/dnSpy.Roslyn/Debugger/GetLocalsAssemblyBuilder.cs index 98331d1697..b55a3e5af9 100644 --- a/dnSpy/Roslyn/dnSpy.Roslyn/Debugger/GetLocalsAssemblyBuilder.cs +++ b/dnSpy/Roslyn/dnSpy.Roslyn/Debugger/GetLocalsAssemblyBuilder.cs @@ -61,7 +61,8 @@ public GetLocalsAssemblyBuilder(LanguageExpressionCompiler language, MethodDef m } else { var methodModule = method.Module; - generatedModule = new ModuleDefUser(Guid.NewGuid().ToString(), Guid.NewGuid(), methodModule.CorLibTypes.AssemblyRef); + generatedModule = new ModuleDefUser(Guid.NewGuid().ToString(), Guid.NewGuid(), + PickCorLibFromAttribute(methodModule) ?? methodModule.CorLibTypes.AssemblyRef); generatedModule.RuntimeVersion = methodModule.RuntimeVersion; generatedModule.Machine = methodModule.Machine; var asm = new AssemblyDefUser(Guid.NewGuid().ToString()); @@ -74,6 +75,29 @@ public GetLocalsAssemblyBuilder(LanguageExpressionCompiler language, MethodDef m } } + static AssemblyRef? PickCorLibFromAttribute(ModuleDef moduleDef) { + var ca = moduleDef.Assembly?.CustomAttributes.Find("System.Runtime.Versioning.TargetFrameworkAttribute"); + if (ca is null) + return null; + if (ca.ConstructorArguments.Count != 1) + return null; + var arg = ca.ConstructorArguments[0]; + if (arg.Type.GetElementType() != ElementType.String) + return null; + var s = (arg.Value as UTF8String)?.String ?? arg.Value as string; + if (s is null) + return null; + var idx = s.IndexOf(','); + if (idx == -1) + return null; + var fw = s.Remove(idx, s.Length - idx).Trim(); + if (fw == ".NETCoreApp") + return moduleDef.GetAssemblyRef("System.Runtime") ?? moduleDef.GetAssemblyRef("System.Private.CoreLib"); + if (fw == ".NETStandard") + return moduleDef.GetAssemblyRef("netstandard"); + return null; + } + GenericParam Clone(GenericParam gp) { var clone = new GenericParamUser(gp.Number, gp.Flags, gp.Name) { Kind = (ITypeDefOrRef?)generatedModule!.Import(gp.Kind)