From 5049968554559a30b1bf71fa5e6bdcc12269c2db Mon Sep 17 00:00:00 2001 From: Jean-Philippe Durot Date: Thu, 20 Nov 2025 18:19:06 -0500 Subject: [PATCH] Fix Assembly property of UnavailableType The assembly was the one in which the type was referenced instead of the assembly containing the type. Issue: #430 Signed-off-by: Jean-Philippe Durot --- ArchUnitNET/Domain/UnavailableType.cs | 14 ++++++++++---- ArchUnitNET/Loader/TypeFactory.cs | 6 +++--- ArchUnitNETTests/Loader/ArchLoaderTests.cs | 5 +++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ArchUnitNET/Domain/UnavailableType.cs b/ArchUnitNET/Domain/UnavailableType.cs index f5dc33ef..a8baf267 100644 --- a/ArchUnitNET/Domain/UnavailableType.cs +++ b/ArchUnitNET/Domain/UnavailableType.cs @@ -47,20 +47,26 @@ private bool Equals(Struct other) return Equals(Type, other.Type); } - public override bool Equals(object obj) + public bool Equals(UnavailableType other) { - if (ReferenceEquals(null, obj)) + if (ReferenceEquals(null, other)) { return false; } - if (ReferenceEquals(this, obj)) + if (ReferenceEquals(this, other)) { return true; } - return obj.GetType() == GetType() && Equals((UnavailableType)obj); + return Type.Equals(other.Type); } + + public override bool Equals(object obj) + { + return obj is UnavailableType unavailableType + && Equals(unavailableType); + } public override int GetHashCode() { diff --git a/ArchUnitNET/Loader/TypeFactory.cs b/ArchUnitNET/Loader/TypeFactory.cs index e21770aa..24059859 100644 --- a/ArchUnitNET/Loader/TypeFactory.cs +++ b/ArchUnitNET/Loader/TypeFactory.cs @@ -347,7 +347,7 @@ TypeReference typeReference ) { var assemblyQualifiedName = System.Reflection.Assembly.CreateQualifiedName( - typeReference.Module.Assembly.FullName, + typeReference.Scope.Name, typeReference.BuildFullName() ); if (_allTypes.TryGetValue(assemblyQualifiedName, out var existingTypeInstance)) @@ -361,8 +361,8 @@ TypeReference typeReference typeReference.BuildFullName(), typeReference.Name, _assemblyRegistry.GetOrCreateAssembly( - typeReference.Module.Assembly.Name.Name, - typeReference.Module.Assembly.FullName, + typeReference.Scope.Name, + typeReference.Scope.ToString(), true, null ), diff --git a/ArchUnitNETTests/Loader/ArchLoaderTests.cs b/ArchUnitNETTests/Loader/ArchLoaderTests.cs index 31aaacdc..5d940802 100644 --- a/ArchUnitNETTests/Loader/ArchLoaderTests.cs +++ b/ArchUnitNETTests/Loader/ArchLoaderTests.cs @@ -131,8 +131,8 @@ public void TypesAreAssignedToCorrectAssemblies() public void UnavailableTypeTest() { // When loading an assembly from a file, there are situations where the assemblies dependencies are not - // available in the current AppDomain. This test checks that the loader does not throw an exception in this - // case. + // available in the current AppDomain. This test checks that the loader does not throw an exception + // and that the unavailable types contain the correct assembly they come from. var currentAssemblyPath = AppDomain.CurrentDomain.BaseDirectory[ ..AppDomain.CurrentDomain.BaseDirectory.IndexOf( @"ArchUnitNETTests", @@ -153,6 +153,7 @@ public void UnavailableTypeTest() var loggerType = architecture.ReferencedTypes.WhereFullNameIs("Serilog.ILogger"); Assert.NotNull(loggerType); Assert.True(loggerType is UnavailableType); + Assert.Equal("Serilog", loggerType.Assembly.Name); } } }