Closed fingers10 closed 9 months ago
TypeDefinition only contains members that are defined in the given type, to get members from a base type, you need to follow the inheritance hierarchy:
You will find a TypeReference
to the base class here:
TypeDefinition.BaseType
to convert it to TypeDefinition
:
TypeDefinition.BaseType?.Resolve()
@NeVeSpl Thanks for the inputs. I have updated my rule as shown below.
public class EncapsulationRule : ICustomRule
{
public bool MeetsRule(TypeDefinition type)
{
return TypeShouldNotHavePublicSetters(type) && TypeShouldNotHavePublicSetters(type.BaseType?.Resolve());
}
private bool TypeShouldNotHavePublicSetters(TypeDefinition? type)
{
return type?.Properties
.All(x => x.SetMethod is null // For IReadOnlyList
|| !x.SetMethod.IsPublic // Allow Private and Protected
|| x.SetMethod.ReturnType.FullName.Contains("IsExternalInit")) // For C# 9 init
?? true;
}
}
First thank you for this wonderful library. I'm using it more. I have added Encapsulation test to domain entities that all should have init, private, IReadonlyList or protected but not public.
Here is the test.
The above code doesn't pull properties from Base Class.
For Example, consider the following
Base Class:
I'm not able to find any properties from
BaseEntity
in theTypeDefinition
of myCustomRule
.Please can you assist me on what I'm missing?