Skip to content

Commit

Permalink
Fix Raw Locals display when multiple core library references are present
Browse files Browse the repository at this point in the history
  • Loading branch information
ElektroKill committed Dec 28, 2023
1 parent b1a859c commit 5bed411
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion dnSpy/Roslyn/dnSpy.Roslyn/Debugger/GetLocalsAssemblyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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)
Expand Down

0 comments on commit 5bed411

Please sign in to comment.