ArchUnitNET cannot detect type dependencies when a class accesses a const field.
When a method accesses a constant (e.g., public const string ConstField = "Value";), the C# compiler replaces the field access with the literal value (using the Ldstr opcode) at compile time. Because the field reference is not present in the IL (Intermediate Language), ArchUnitNET's dependency analysis cannot trace the dependency back to the class containing the constant field.
If you attempt to assert that a method has a type dependency on a class containing only constants, the assertion will fail.
class ClassWithStaticField
{
public const string ConstField = "ConstField";
}
class ClassAccessingField
{
public void MethodAccessingConstField()
{
// The compiler replaces this with the literal string,
// removing the dependency on ClassWithStaticField in the IL.
var a = ClassWithStaticField.ConstField;
}
}
// This assertion will FAIL:
var method = Architecture.GetClassOfType(typeof(ClassAccessingField))
.GetMethodMembers()
.First(member => member.FullNameContains("MethodAccessingConstField"));
var methodTypeDependencies = method.GetTypeDependencies().ToList();
Assert.Contains(Architecture.GetClassOfType(typeof(ClassWithStaticField)), methodTypeDependencies);