Skip to content

Commit

Permalink
Fix #92 - Ignore custom attribute errors when it comes to assembly re…
Browse files Browse the repository at this point in the history
…solution.
  • Loading branch information
brutaldev committed Jan 21, 2023
1 parent bfba843 commit 7daf240
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/Brutal.Dev.StrongNameSigner/SigningHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,30 +311,37 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
// Fix CustomAttributes with Type references.
foreach (var assembly in allAssemblies)
{
foreach (var constructorArguments in assembly.Definition.CustomAttributes
.Where(attr => attr.HasConstructorArguments)
.Select(attr => attr.ConstructorArguments)
.ToList())
foreach (var customAttribute in assembly.Definition.CustomAttributes.ToList())
{
foreach (var argument in constructorArguments.ToArray())
try
{
if (argument.Type.FullName == "System.Type" && argument.Value is TypeReference typeRef)
if (customAttribute.HasConstructorArguments)
{
var signedAssembly = assembliesToProcess.FirstOrDefault(a => a.Definition.Name.Name == typeRef.Scope.Name);

if (signedAssembly != null)
foreach (var argument in customAttribute.ConstructorArguments.ToArray())
{
Log($" Fixing {signedAssembly.Definition.Name.Name} reference in CustomAttribute in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");
if (argument.Type.FullName == "System.Type" && argument.Value is TypeReference typeRef)
{
var signedAssembly = assembliesToProcess.FirstOrDefault(a => a.Definition.Name.Name == typeRef.Scope.Name);

if (signedAssembly != null)
{
Log($" Fixing {signedAssembly.Definition.Name.Name} reference in CustomAttribute in assembly '{tempFilePathToInputOutputFilePairMap[assembly.FilePath].InputFilePath}'.");

var updatedTypeRef = signedAssembly.Definition.MainModule.GetType(typeRef.FullName);
var updatedTypeRef = signedAssembly.Definition.MainModule.GetType(typeRef.FullName);

var updatedArgument = new CustomAttributeArgument(argument.Type, updatedTypeRef);
var idx = constructorArguments.IndexOf(argument);
constructorArguments.RemoveAt(idx);
constructorArguments.Insert(idx, updatedArgument);
var updatedArgument = new CustomAttributeArgument(argument.Type, updatedTypeRef);
var idx = customAttribute.ConstructorArguments.IndexOf(argument);
customAttribute.ConstructorArguments.RemoveAt(idx);
customAttribute.ConstructorArguments.Insert(idx, updatedArgument);
}
}
}
}
}
catch (AssemblyResolutionException ex)
{
Log($" Failed to check custom attribute '{customAttribute.AttributeType.FullName}': {ex.Message}");
}
}
}

Expand Down Expand Up @@ -464,7 +471,6 @@ public static void SignAssemblies(IEnumerable<InputOutputFilePair> assemblyInput
// Restore the backup that would have been created above.
File.Copy(inputOutpuFilePair.BackupAssemblyPath, inputOutpuFilePair.InputFilePath, true);
File.Delete(inputOutpuFilePair.BackupAssemblyPath);

}
catch (IOException ioex)
{
Expand Down

0 comments on commit 7daf240

Please sign in to comment.