Skip to content

Commit ba133fb

Browse files
committed
Fix issue #107 by handling custom attribute fixing for multiple versions of the same assembly with the same name.
1 parent d73ff66 commit ba133fb

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/Brutal.Dev.StrongNameSigner.Setup/StrongNameSigner.nuspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ The tool will also re-write the assembly references (as well as any InternalsVis
2020
</description>
2121
<summary>Automatic strong-name signing of referenced assemblies. Utility software to strong-name sign .NET assemblies, including assemblies you do not have the source code for.</summary>
2222
<releaseNotes>
23-
Update custom attribute fixing to include every custom attribute (PR #104).
24-
Add more environment specific resolution paths to try and solve (Issue #103/105).
23+
Fix issue #107 by handling custom attribute fixing for multiple versions of the same assembly with the same name.
2524
</releaseNotes>
2625
<icon>images\Icon.png</icon>
2726
<license type="file">docs\LICENSE.md</license>

src/Brutal.Dev.StrongNameSigner/SigningHelper.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,19 @@ public static bool SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
497497

498498
private static void FixCustomAttributes(HashSet<AssemblyInfo> allAssemblies, Dictionary<string, InputOutputFilePair> tempFilePathToInputOutputFilePairMap, ref bool hasErrors)
499499
{
500-
var assembliesByName = allAssemblies.ToDictionary(a => a.Definition.Name.Name);
500+
var assembliesByName = new Dictionary<string, List<AssemblyInfo>>();
501+
foreach (var assembly in allAssemblies)
502+
{
503+
if (!assembliesByName.TryGetValue(assembly.Definition.Name.Name, out var value))
504+
{
505+
assembliesByName.Add(assembly.Definition.Name.Name, [assembly]);
506+
}
507+
else
508+
{
509+
value.Add(assembly);
510+
}
511+
}
512+
501513
foreach (var assembly in allAssemblies)
502514
{
503515
var types = assembly.Definition.Modules
@@ -561,7 +573,7 @@ private static void FixAttributes(
561573
IEnumerable<CustomAttribute> customAttributes,
562574
AssemblyInfo assembly,
563575
Dictionary<string, InputOutputFilePair> tempFilePathToInputOutputFilePairMap,
564-
Dictionary<string, AssemblyInfo> assembliesByName,
576+
Dictionary<string, List<AssemblyInfo>> assembliesByName,
565577
ref bool hasErrors)
566578
{
567579
foreach (var customAttribute in customAttributes)
@@ -572,20 +584,18 @@ private static void FixAttributes(
572584
{
573585
foreach (var argument in customAttribute.ConstructorArguments.ToArray())
574586
{
575-
if (argument.Type.FullName == "System.Type" && argument.Value is TypeReference typeRef)
587+
if (argument.Type.FullName == "System.Type" && argument.Value is TypeReference typeRef && assembliesByName.TryGetValue(typeRef.Scope.Name, out var value))
576588
{
577-
if (assembliesByName.TryGetValue(typeRef.Scope.Name, out var signedAssembly))
589+
foreach (var signedAssembly in value.Select(sa => sa.Definition))
578590
{
579-
Log($" Fixing {signedAssembly.Definition.Name.Name} reference in CustomAttribute in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
591+
Log($" Fixing {signedAssembly.Name.Name} reference in CustomAttribute in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
580592

581-
var updatedTypeRef = signedAssembly.Definition.MainModule.GetType(typeRef.FullName);
593+
signedAssembly.MainModule.GetType(typeRef.FullName);
582594

583595
// Import the type reference into the current module, so it gets the correct scope.
584596
// Without this import, the type reference in the ILASM will only point to the type (like "Brutal.Dev.StrongNameSigner.TestAssembly.A.A")
585597
// instead of the full assembly-qualified name (like "Brutal.Dev.StrongNameSigner.TestAssembly.A.A, Brutal.Dev.StrongNameSigner.TestAssembly.A, Version=1.0.0.0, PublicKeyToken=...").
586-
var importedTypeRef = assembly.Definition.MainModule.ImportReference(
587-
signedAssembly.Definition.MainModule.GetType(typeRef.FullName)
588-
);
598+
var importedTypeRef = assembly.Definition.MainModule.ImportReference(signedAssembly.MainModule.GetType(typeRef.FullName));
589599

590600
var updatedArgument = new CustomAttributeArgument(argument.Type, importedTypeRef);
591601

0 commit comments

Comments
 (0)